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



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


-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"?

  • I'm on OS X if that matters. – The Unfun Cat Apr 24 '13 at 7:21
  • 1
    ls *s@([123]|19)r*? – manatwork Apr 24 '13 at 7:23
  • ls s@([123]|19)r gives '-bash: syntax error near unexpected token `('' – The Unfun Cat Apr 24 '13 at 7:24
  • Which bash version? Are you trying in interactive or non-interactive shell session? pastebin.com/VSSTt6qC – manatwork Apr 24 '13 at 7:29
  • 1
    @TheUnfunCat You need to enable the cool stuff with shopt -s extglob – Hauke Laging Apr 24 '13 at 7:37

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

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

                  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.


You might want to try this:

ls *s?([123])r* *s19r*

For example

I have a directory with the following files:

% ls | column
s10r    s12r    s14r    s16r    s18r    s1r s2r s4r s6r s8r
s11r    s13r    s15r    s17r    s19r    s23r    s3r s5r s7r s9r

Using the above mentioned ls:

% ls *s?([123])r* *s19r*
s19r  s1r  s2r  s3r

See the bash manual on pattern matching for more details.

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.