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
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.
You can always use sort(1) and its numeric sort:
for i in $(ls -1 * | sort -n) ; do echo $i ; done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With