The following works lists all *.txt
files in the current dir, except those that begin with a number.
This works in bash
, dash
, zsh
and all other POSIX compatible shells.
for FILE in /some/dir/*.txt; do # for each *.txt file
case "${FILE##*/}" in # if file basename...
[0-9]*) continue ;; # starts with digit: skip
esac
## otherwise, do stuff with $FILE here
done
In line one the pattern /some/dir/*.txt
will cause the for
loop to iterate over all files in /some/dir
whose name end with .txt
.
In line two a case statement is used to weed out undesired files. – The ${FILE##*/}
expression strips off any leading dir name component from the filename (here /some/dir/
) so that patters can match against only the basename of the file. (If you're only weeding out filenames based on suffixes, you can shorten this to $FILE
instead.)
In line three, all files matching the case
pattern [0-9]*
) line will be skipped (the continue
statement jumps to the next iteration of the for
loop). – If you want to you can do something more interesting here, e.g. like skipping all files which do not start with a letter (a–z) using [!a-z]*
, or you could use multiple patterns to skip several kinds of filenames e.g. [0-9]*|*.bak
to skip files both .bak
files, and files which does not start with a number.