1

Hi I have the following files, whose names contain spaces, on a Linux machine. I need to ls them in numerical order

myfile_2020-Jul-06 00-00-00_1.csv
myfile_2020-Jul-06 00-00-00_2.csv
myfile_2020-Jul-06 00-00-00_3.csv
myfile_2020-Jul-06 00-00-00_4.csv
myfile_2020-Jul-06 00-00-00_5.csv
myfile_2020-Jul-06 00-00-00_10.csv

When I try ls -1 -- *.csv | sort -n or ls-lart the _10 file is always sorted first. How do ls and sort these in the correct numerical order with _10.csv at the bottom?

ls -1 -- *.csv | sort -n

myfile_2020-Jul-06 00-00-00_10.csv
myfile_2020-Jul-06 00-00-00_1.csv
myfile_2020-Jul-06 00-00-00_2.csv
myfile_2020-Jul-06 00-00-00_3.csv
myfile_2020-Jul-06 00-00-00_4.csv
myfile_2020-Jul-06 00-00-00_5.csv 
1
  • 2
    Try sort -t_ -k3,3n to sort numerically by the 3rd field of underscore-separated records. Jul 8, 2020 at 17:13

1 Answer 1

2

There is this option for GNU ls:

ls -v

Per man page for ls:

-v natural sort of (version) numbers within text

1
  • 1
    Thank you @Sonia! it worked :-) Jul 8, 2020 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .