Alcott
Alcott

Reputation: 18585

`sort` file names which contain chars and numbers

I have many related files, like this:

data0.csv data1.csv data2.csv data11.csv data21.csv

when I ls ., they look like this:

data0.csv
data1.csv
data11.csv
data2.csv
data21.csv

but I want them in ascending order like this:

data0.csv
data1.csv
data2.csv
data11.csv
data21.csv

How to do it in bash?

Upvotes: 1

Views: 224

Answers (2)

potong
potong

Reputation: 58371

This might work for you:

ls -v *.csv

Upvotes: 3

For this particular example you could also do this:

$> ls . | sort -k3 -ta -n
data0.csv
data1.csv
data2.csv
data11.csv
data21.csv

Sorting numerically (-n) on the third field (-k3) using 'a' as the field separator (-ta).

Upvotes: 2

Related Questions