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 '_'


This is a generic answer! You have to apply rules to the specific set of data

ls | sort

Example:

ls | sort -n -t _ -k 2

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

Tags:

Sorting

Bash