'ls -l' in groovy
I need to display name, size, date of 开发者_如何学Gofiles using ls -l
unix command in groovy .
How can we run ls -l
in groovy to view info ?
thanks in advance.
"ls -l".execute().text
Should do it
def list = 'ls -l'.execute().text
list.eachLine{
// code goes here
}
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.
精彩评论