I created 3 files in a directory with the following names:
11 13 9
Problem, as I created file 9
after 13
it is placed after file 13
when I do the ls
command.
Do you have any idea to make the ls
command sort the files in the numerical order, like this:
9 11 13
In order to ls by date or list Unix files in last modifed date order use the -t flag which is for 'time last modified'. or to ls by date in reverse date order use the -t flag as before but this time with the -r flag which is for 'reverse'.
As we already mentioned, by default, the ls command is listing the files in alphabetical order.
ls command sort by time with -lht options in Linux The best way to sort by time in Linux ls command is using ls with -lht options. Open the terminal and type ls -lht. The most recently modified file will be listed on the first. The output is in the long format and the file size is in human readable format.
To sort by number pass the -n option to sort . This will sort from lowest number to highest number and write the result to standard output. Suppose a file exists with a list of items of clothing that has a number at the start of the line and needs to be sorted numerically.
Look at the man
page for ls
.
From there-
-v natural sort of (version) numbers within text.
You'll find that the command ls -v
does what you want.
If you want the file names on different lines then you can do ls -1v
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With