Q:

sorting files in a directory based on numerical index within file-names

I have used a for loop in my script like this ...
 for var in ipsec_packet*.txt; do
 echo $var
 done

Output
ipsec_packet10.txt
ipsec_packet11.txt
ipsec_packet12.txt
ipsec_packet13.txt
ipsec_packet14.txt
ipsec_packet15.txt
ipsec_packet16.txt
ipsec_packet17.txt
ipsec_packet18.txt
ipsec_packet1.txt
ipsec_packet2.txt
ipsec_packet3.txt
ipsec_packet4.txt
ipsec_packet5.txt
ipsec_packet6.txt
ipsec_packet7.txt
ipsec_packet8.txt
ipsec_packet9.txt

but I want them starting from 1 to the largest avaliable (here 18) in sorted order like this ..
ipsec_packet1.txt
ipsec_packet2.txt
... 
...
ipsec_packet18.txt

I tried sort -n k14 but it did not help. Please suggest me some variation of sort or any other bash/awk feature which could help me.

A:

You can try using sort with the -V option used for natural sorting of numbers within text:
for var in `ls ipsec_packet*.txt | sort -V`; do  echo $var; done

