tags:

views:

77

answers:

3

I need to display name, size, date of files using ls -l unix command in groovy .

How can we run ls -l in groovy to view info ?

thanks in advance.

+1  A: 
"ls -l".execute().text

Should do it

DRL
@DamienL, yes. thanks
Srinath
+3  A: 
def list = 'ls -l'.execute().text
list.eachLine{
    // code goes here
}
Michael Borgwardt
@Michael, thanks
Srinath
+1  A: 

If you don't mind restricting yourself to the file properties that Java knows about, you can do this in a more portable, flexible, secure and efficient way using methods of the File class.

File dir = new File(".")
dir.eachFile { f ->
   println "${f} ${f.size()} ${new Date(f.lastModified())}"
}

Check both the GroovyDocs and the JavaDocs for File to see all the ways you can filter files, and all the properties you have access to.

Of course you could have any code in that block, replacing println.

In the Perl world, we learned that invoking shell commands was usually to be avoided, when native Perl was an option. This is even more true in Groovy, I'd argue. Of course, you might have a special requirement, where you need the exact output 'ls -l' would produce.

slim
@slim, yes my requirement is to produce exact output. But i would like to consider your comment. my requirement may change now . thanks for the comment
Srinath