Reputation: 1635
Is there any way to control the sorting that occurs when I run a, for i in * ; do; echo $i; done; in my directory. It seems to always use ascii sorting. Anyway to do a numeric sort?
Thanks, Vivek
Upvotes: 6
Views: 7578
Reputation: 37298
How about making your filenames so they sort naturally as numbers, i.e. padded with leading zeros. Instead of 1 .. 10 .. 100
, use 001 ..010 .. 100
?
To include sub-directories:
for i in $( ls -d * | sort -n ); do echo $i; done;
To exclude sub-directories:
for i in $( ls | sort -n ); do echo $i; done;
I hope this helps.
Upvotes: 7
Reputation: 1826
you can use the ordering from ls [sorting options] that you give for the directory with i.e.
for i in `ls -X` ; do
echo $i
done
first the ls command with a certain sorting is executed, in the example case sorting for extension then you give out the the list with a for loop
Upvotes: 0
Reputation: 6555
You can always use sort(1) and its numeric sort:
for i in $(ls -1 * | sort -n) ; do echo $i ; done
Upvotes: 6