File Sorting – List Files Sorted Numerically

filenameslsnumberingsort

I have a bunch of files from log1 to log164.

I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this:

home:logs Home$ ls -1 | sort
log1.gz
log10.gz
log100.gz
log101.gz
log102.gz
log103.gz
log104.gz
log105.gz
log106.gz
...etc

What I want is

home:logs Home$ ls -1 | sort
log1.gz
log2.gz
log3.gz
log4.gz
log5.gz
log6.gz
log7.gz
...{more here}
log99.gz
log100.gz
log101.gz
log102.gz
...etc

Any suggestions in what I could use to do this?

Best Answer

bash's braces, {}, will enumerate them in order:

for file in log{1..164}.gz; do
    process "$file"
done
Related Question