Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash true numerical order

Tags:

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

like image 806
johnlemon Avatar asked Nov 20 '10 18:11

johnlemon


People also ask

How do I sort by number in bash?

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.

How do I sort in bash?

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 ).

How do I sort in reverse order in Linux?

-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.


1 Answers

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 
like image 66
icanhasserver Avatar answered Sep 23 '22 17:09

icanhasserver