List files sorted numerically
Why not use the built-in ls
feature for this particular case:
-v natural sort of (version) numbers within text
For example ls -1v log*
With GNU ls (i.e. on Linux, Cygwin, or other systems that have GNU ls specifically installed):
ls -v
In zsh:
echo *(n)
In other shells:
echo log?.gz log??.gz log???.gz
Replace echo
by printf '%s\n'
if you want each file name on a separate line.
If you want file metadata as well (ls -l
) and you don't have GNU ls, you'll need to call ls
separately for each file name or group of file names that you want to see in lexicographic order.
ls -ld log?.gz; ls -ld log??.gz; ls -ld log???.gz
To avoid these difficulties, use enough leading zeroes in your file names so that the lexicographic sort is human-friendly (log001.gz
, etc).
bash
's braces, {}
, will enumerate them in order:
for file in log{1..164}.gz; do
process "$file"
done