Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort files in a folder using bash?

I have these files in a folder:

chap11-solutions.pdf
chap12-solutions.pdf
chap13-solutions.pdf
chap14-solutions.pdf
chap15-solutions.pdf
chap16-solutions.pdf
chap17-solutions.pdf
chap21-solutions.pdf
chap22-solutions.pdf
chap23-solutions.pdf
chap24-solutions.pdf
chap25-solutions.pdf
chap26-solutions.pdf
chap2-solutions.pdf
chap3-solutions.pdf
chap4-solutions.pdf
chap5-solutions.pdf
chap6-solutions.pdf
chap7-solutions.pdf
chap8-solutions.pdf
chap9-solutions.pdf

how do I sort them in this way: chap1..., chap...2, ...., chap11..., chap12,... using Ubuntu bash shell? Thanks.

like image 572
wakandan Avatar asked Jan 23 '11 13:01

wakandan


People also ask

How do I sort files in a folder in Linux?

If you want to list files by owner (e.g., in a shared directory), you can pass the output of the ls command to sort and pick out the owner column by adding -k3 to sort on the third field. In fact, you can sort on any field this way (e.g., year).

How do you sort files in shell script?

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. 3. -n Option: To sort a file numerically used –n option.

How do I sort the types of files in a directory?

You can reveal more options from File Explorer's View tab. In the Current view section, click or tap on Sort by. Same as before, the sorting options displayed are specific to that folder's view template. To add more criteria to the Sort by menus, click or tap Choose columns.

How do I sort a file in Linux terminal?

Sort a File Numerically To sort a file containing numeric data, use the -n flag with the command. By default, sort will arrange the data in ascending order. If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.


1 Answers

ls|sort -V

The -V parameter ensures that chap10 is considered upper that chap9.

like image 77
Arnaud Le Blanc Avatar answered Oct 08 '22 00:10

Arnaud Le Blanc