The standard file name globbing pattern to match a digit is [0-9]
. This matches a single digit:
cat file[0-9].txt
To select only two of these:
cat file[25].txt
For larger numbers than 9, brace expansion will be useful (but see note below for the difference between globbing patterns and brace expansions):
cat file{25..60}.txt
Again, brace expansion allows for individual numbers as well:
cat file{12,45,900,xyz}.txt
(note that in the example above, the brace expansion does not involve an arithmetic loop, but just generates names based on the strings provided).
In bash
, with the extglob
shell option enabled (shopt -s extglob
), the following will also work:
cat file@(12|45|490|foo).txt
The @(...)
pattern will match any one of the included |
-delimited patterns.
The difference between globbing patterns as [...]
and @(...)
and brace expansions, is that a brace expansion is generated on the command line and may not actually match any existing names in the current directory. A filename globbing pattern will match names, but the shell won't complain if not all possible name exist. If no matching name exists, the pattern will remain be unexpanded, unless also the nullglob
shell option is set, in which case the pattern is removed.
Example:
touch file1
ls file[0-9]
Here, only the file listing for file1
will be shown.
With ls file{0..9}
, ls
would complain about not finding file0
, file2
etc.
In the following example, the first command will only touch existing names that matches the given pattern, while the second line will create files that does not already exist:
touch file[0-9]
touch file{0..9}