How can I order files in a directory by their true numeric order.
file1.txt file2.txt file11.txt ...
I think it's called : Natural Order
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.
Using Bash Sort to Order Files by Size To sort files by size, pass the -S flag to tell the ls command to sort the list of files by file size. Run the command below to list files ( ls ) sorted by file size in a long list format ( -lS ).
-r Option: Sorting In Reverse Order: You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default. Example: The input file is the same as mentioned above.
Use the -v
option:
ls -v file* file1 file2 file11 file12
Another option may be using sort -V
, assuming that one is available on your platform:
ls file* |sort -V
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