How to list files in ascending order?

Hi,
I need to list files in ascending order. Filenames are in format inpTDT_1, inpTDT_2, inpTDT_3 and so on.
I want to list them in the ascending order based on the digit after underscore and send the output to a file.
Please help

ls inpTDT_* | sort -nk1.8

Or, if the prefix before _ is variable, you can use sort -t_ -nk2

I think we can use only "sort -n" command.

ls TDT_*|sort -n

No, a numeric sort on the whole input line won't work:

$ ls inpTDT_* | sort -n | head -10    
inpTDT_1
inpTDT_10
inpTDT_100
inpTDT_1000
inpTDT_101
inpTDT_102
inpTDT_103
inpTDT_104
inpTDT_105
inpTDT_106
$ ls inpTDT_* | sort -nk1.8 | head -10
inpTDT_1
inpTDT_2
inpTDT_3
inpTDT_4
inpTDT_5
inpTDT_6
inpTDT_7
inpTDT_8
inpTDT_9
inpTDT_10

Thanks Scott. yes, agreed.

I thought only few files like TDT_1 to TDT_3. :slight_smile: