I know that Regular expressions are to be used only with characters and strings, but sometimes I find them in the names of files. My question is: are regexes only used with characters or also with filenames?

2 Answers 2


Regular expressions and file name globbing are two very different things.

Regular expressions are used in commands / functions for pattern matching in text. For example in the pattern parameter of grep, or in programming languages.

File name globbing is used by shells for matching file and directory names using wildcards. The capabilities of globbing depend on the shell. Bash, for example, supports wildcards like:

  • * match 0 or more characters
  • ? match 1 character
  • [...] match a character in the specified set

These wildcards may look similar to regular expressions, indeed [...] has the same meaning in globbing and regex. But * and ? mean different things in globbing and regex.

In a comment you wrote:

but how the interpreter difference * whether it's a joker or regex ? for example : grep a*b a*.txt ?

Easy. Sort of.

First of all, the shell tries to interpret the wildcards, by matching them against filenames. If there are files starting with "a" and ending with "b", the shell will replace a*b with the matching filenames. Same goes for a*.txt. If there are no matching filenames, the shell will pass the arguments to grep as they were, literally.

However, the first parameter of grep should be a pattern. In 99.999% of practical use cases you don't want the first parameter to be interpreted by the shell. So most probably the intention was this:

grep "a*b" a*.txt

Thanks to quoting a*b, the shell will not interpret it using globbing, and instead pass it directly to grep. In turn, grep will interpret that as a regular expression (by design).

To sum it up, the shell interprets the command line following its own globbing language, which is using wildcards. Commands, programs interpret their parameters in whatever way they were designed by their authors.


If a filename is a string for you, you can use regex also for filenames. For example: if you want to find filenames matching a regex you can try:

find ./ -regex '.*[abc][xyz].*'

The command looks for files which have a, b or c in the name followed by x, y or z. This is just one example. There are endless possibilites.

  • but how the interpreter difference * whether it's a joker or regex ? for example : $ grep a"asterix"b a"asterix".txt
    – Hamza
    Commented Dec 29, 2015 at 10:02
  • 3
    If you use find ./ -name 'a*' you use * as a wildcard. When you type find ./ -regex 'a*', you use * in a regex. The important difference is the switch -name or -regex.
    – nobody
    Commented Dec 30, 2015 at 7:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .