I want to select all files in a directory that end with any of the following extensions: txt, java, xml, csv

Right now I'm doing like this:

echo *.{txt,java,xml,csv}

The problem is, if the directory does not contain, say, a .java file, the output looks like this:

f0030720.txt f0033510.txt f1028864.txt f1029056.txt f1068796.txt *.java f0905776.xml f1067014.csv f1067046.csv f1067056.csv f1067074.csv

That *.java item creates problems if I use mv, tar, or anything like that, since it's not actually a file. Is there any way for me to ignore any globs that don't actually match a file?


Set the nullglob option:

shopt -s nullglob
  • 1
    And if you're using zsh, (N) at the end (e.g. foo*bar(N)) does it for just that glob. [And (^N) disables it] – Kevin Oct 14 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.