Bash – Ignore globs that don’t match anything

bashshellwildcards

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?

Best Answer

Set the nullglob option:

shopt -s nullglob
Related Question