I would like to write a while loop in Bash that runs over all instances of files that are of the form of
{number}.tst
for example, 1.tst
, 2.tst
, ... 50.tst
.
I do not want it to run over the file tst.tst
.
How would I go about writing this? I assume I will need a boolean phrase and [0-9]*
somewhere in there, but I am not entirely sure on the syntax.
If you only need to exclude alphabetic names like your example
tst.tst
you could use a simple shell globWith bash extended globs (which should be enabled by default in Ubuntu)
given
then
+([0-9])
means one or more decimal digits:You can check whether extended globbing is enabled using
shopt extglob
and set it if necessary usingshopt -s extglob
(and unset usingset -u extglob
).From this Stack Overflow answer: List files that only have number in names:
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 toxargs -0
or even (using Bash):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:
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:
If you want to process the files in numerical order, there's the
seq
command (readman seq
).