Shells have wildcard characters that differ from the usual regexp syntaxes: ?
to match any single character, *
to match any number of characters, and [abc]
to match any single character among a
, b
or c
. The following command shows all files whose name matches the extended regular expression¹ ((R|r)eading(T|t)est(D|d)ata)
in the current directory:
echo *[Rr]eading[Tt]est[Dd]ata*
If you want to find files in subdirectories as well, then first run shopt -s globstar
(you can put this command in your ~/.bashrc
). This turns on the **
pattern to match any level of subdirectories:
echo **/*[Rr]eading[Tt]est[Dd]ata*
Shell wildcard characters are not as powerful as regular expressions. For example, there is no or (|
) operator. You can get the power of regular expressions, but with a different syntax for historical reasons. Add shopt -s exgblob
to your .bashrc
, then you can use @(foo|bar)
to match foo
or bar
(like foo|bar
in an ERE), *(pattern)
to match a sequence any number of occurrences of pattern
(like (pattern)*
in an ERE), +(pattern)
to match one or more occurrences, ?(pattern)
to match zero or one occurrence, and !(pattern)
to match anything except pattern
(no ERE equivalent).
¹ “Extended regular expression” (ERE for short) is the unix name of the regex syntax that JavaScript uses.
ls -l *[Rr]eading[Tt]est[Dd]ata*
– cas Aug 26 '12 at 22:33ls: cannot access *[Rr]eading[Tt]est[Dd]ata*: No such file or directory
even though there is a file on my system that matches the regex. – Anderson Green Aug 27 '12 at 05:37ls -l *.js*
and got the same output:No such file or directory
. I expected to see a list of every file that ended in .js, but it didn't work as expected. – Anderson Green Aug 27 '12 at 05:41