With zsh
:
set -o extendedglob
print -rC1 -- file(<0-500>~^*[13579]).txt
Note that {0..50}
(from zsh) is not a glob operator, it's brace expansion, that expands regardless of whether corresponding files exist or not.
<0-500>
(zsh specific, not copied by other shells yet) is a glob operator, it matches on sequences of ASCII decimal digits that make up a number between 0 and 500.
~
is the except (and not) operator and ^
is not. A~B
is A and not B, A~^B
is A and not not B, so A and B.
In bash
, you could do something similar with:
shopt -s failglob extglob
printf '%s\n' file*(0)[01234][0123456789][13579].txt
*(0)
(a extended glob operator from ksh) being any number of zeros, and then we expect a digit from 0 to 4, one from 0 to 9 and one in the 13579 set.
Now, for brace expansion to expand to 1, 3, ... 499 regardless of whether corresponding files exist, in both zsh and bash (and ksh93¹ and yash -o braceexpand
):
printf '%s\n' file{1..499..2}.txt
Other notes:
[1,3,5,7,9]
is the same as [13579,]
, it matches on either of the characters in that set. You want [13579]
instead.
- in bash (not zsh),
[0-9]
generally matches on hundreds of different digit-like characters. Use [0123456789]
if you only want to match on those 10 ASCII decimal characters.
- Beware that if you don't pass the
-d
option to ls
and any of the filexxx.txt
files you pass to it (as the result of the shell's brace expansion or filename generation aka globbing) is of type directory, ls
will list the contents of the directory instead. You almost always want to use -d
when passing glob expansions to ls
. Though, here, ls
will just end up sorting and printing the list given by the shell (after having verified that the files exist) so is mostly superflous. You might as well use the print
or printf
utilities to just print that list.
¹ {a,b}
brace expansion is from csh in the late 70s, zsh extended it with {4..10}
(and {a-zXYZ}
with braceccl
) in the early 90s), ksh93 extended it to {1..400..2%04d}
in the mid-2000s, the {1..400.2}
part made it back to other shells, the %04d
one is still specific to ksh93 AFAIK, though zsh had {0001..0400}
for that from the start and generally has separate operators for padding.
{x..y[..incr]}
– steeldriver Apr 27 '22 at 11:33