Bash and sort files in order Bash and sort files in order bash bash

Bash and sort files in order


ls data_* | sort -n -t _ -k 2

-n: sorts numerically
-t: field separator '_'
-k: sort on second field, in your case the numbers after the first '_'


How about using the -v flag to ls? The purpose of the flag is to sort files according to version number, but it works just as well here and eliminates the need to pipe the result to sort:

ls -lv data_*


If your sort has version sort, try:

ls -1 | sort -V

(that's a capital V).