I have a directory containing three files, for example, 5.war
, 6.war
and 7.war
.
What command will return the file with the "smallest" filename from this directory? (In this case, 5.war
.)
I have a directory containing three files, for example, 5.war
, 6.war
and 7.war
.
What command will return the file with the "smallest" filename from this directory? (In this case, 5.war
.)
ls
sorts by name by default. If there's no directories to contend with, then just do this:
ls | head -1
To add some completion, if you have to worry about directories, do the following:
ls -p | egrep -v /$ | head -1
Unfortunately, ls
sorts the file names alphabetically, which isn't correct for numbers. For example, 10.war
will before 2.war
, which is not what you wish.
Probably you can find some, more intelligent sorting tool by google, but in the absence of that I would suggest:
head -1
, I suggest a numerical sort: ls|sort -n|head -1