Shell – “or” in shell glob

osxshellshell-scriptwildcards

I want to ls the files containing the substring "s1r", "s2r", "s3r" or "s19r" in their filenames.

I'm almost there!

Botched attempts:

ls *s[123][9?]r*

The above gives me only files including the substring

s19r

While

ls *s[1-3]|[19]r*

returns

-bash: [19]r*: command not found

ls: *s[1-3]: No such file or directory

That is, it does not recognize the or | operator – which makes sense as it is also used to pipe.

How do I ls the files containing "s1r", "s2r", "s3r" or "s19r"?

Best Answer

Use ls *s1r* *s2r* *s3r* *s19r*.

If you care about non existing files you can set the nullglob option:

          nullglob
                  If  set,  bash allows patterns which match no files (see
                  Pathname Expansion above) to expand to  a  null  string,
                  rather than themselves.

If your shell is not bash, there is probably a similar way. Have a look at it's man page.

Related Question