ls ignore "no matches"

In

ls -d ./foldername/*.{a,b,}test

{a,b,...} is not a glob operator, that's brace expansion, that's first expanded to:

ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test

And each glob expanded individually, and if any glob doesn't match, the command is cancelled as you'd expect in zsh (or fish; in bash, you need the failglob option to get a similar behaviour).

Here, you'd want to use a single glob that matches all those files, and only cancel the command if that one glob didn't match any file:

ls -d ./foldername/*.(a|b|)test

You don't want to use nullglob, as if none of the globs matched, it would run ls without arguments, so list the current directory. cshnullglob is better in that regard as it removes non-matching globs but still cancels the command if all the globs fail to match.

You wouldn't want to use nonomatch, as that would give you the broken behaviour of bash which would be a shame.

For a glob alternative that works in both zsh and bash, you could use the ksh globs (set -o kshglob in zsh and shopt -s extglob in bash).

Then, you'd do:

ls -d ./foldername/*.@(a|b|)test

or:

ls -d ./foldername/*.?([ab])test

Add the failglob option in bash to avoid the glob being passed literally to ls when it doesn't match.

See Why is nullglob not default? for more information.


It may be best to do this with find:

find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'