Running a Bash while loop over all similar files

If you only need to exclude alphabetic names like your example tst.tst you could use a simple shell glob

for f in [0-9]*.tst; do echo "$f"; done

With bash extended globs (which should be enabled by default in Ubuntu)

given

$ ls *.tst
1.tst  2.tst  3.tst  4.tst  50.tst  5.tst  bar.tst  foo.tst

then +([0-9]) means one or more decimal digits:

for f in +([0-9]).tst; do echo "$f"; done
1.tst
2.tst
3.tst
4.tst
50.tst
5.tst

You can check whether extended globbing is enabled using shopt extglob and set it if necessary using shopt -s extglob (and unset using set -u extglob).


From this Stack Overflow answer: List files that only have number in names:

find . -regex '.*/[0-9]+\.tst'

OR

Using find also has advantages when you want to do something with the files, e.g. using the built-in -exec, -print0 and pipe to xargs -0 or even (using Bash):

while IFS='' read -r -d '' file
do
  # ...
done < <(find . -regex '.*/[0-9]+\.tst' -print0)

Note the other answers here my include files that aren't numbers if the filename starts with a digit. The answer posted here does not though. For example:

$ ls *.tst
12tst.tst  1.tst  2.tst

$ find . -maxdepth 1 -regex '.*/[0-9]+\.tst'
./1.tst
./2.tst

NOTE: Use -maxdepth 1 argument to only list numbered files in the current directory and not in sub-directories.


In this case, there are no filenames of the form {number}{non-number}.tst, so one possible solution is to include all the filenames that start with a number:

for filename in [0-9]*.tst; do
    echo "$filename"  # Example command
done