Do you know how to search for files using special patterns made up of characters like *, {}, ? and []? Well, there is a thing that uses such patterns to search for files. It is called globbing or in other words, glob patterns. Globbing is a built-in feature of many shells in Unix-like systems and in Windows as well. Let's take a closer look at them using the popular Unix shell – Bash, as an example.
Glob exposition
Globbing is a feature that can match patterns to file names or path names. To do so, it uses a set of wildcards — symbols, which can represent other characters or no characters at all.
The snippet below shows how you can match all file names ending with the letter 'k'.
$ ls
Brunt Damar Dukat Garak Martok Quark Weyoun
$ ls *k
Garak Martok Quark
You can see that *k matches file names whose last letter is 'k'. Here, * holds special meaning. Let's look at other special characters you can use to search for files.
Glob patterns
The table below lists the most common wildcards used for globbing.
* |
Any character (except /) in any number, or no characters at all |
? |
Any one character |
[] or {} |
A group of characters like {file1, file3, file6} or a set of them like [0-9] [a-zA-Z] or [p1K] |
[!] or [^] |
Negation of group or set of characters; is used with square brackets, like [!1-5] |
As we have seen earlier you can use glob patterns with such commands in the terminal as ls or cp, mv, rm and others, including even git.
rm file{1-6}
cp ~/.* /mnt/usb/
git add *.scm
Now, let's compare globbing with regular expressions.
Globbing and regular expressions
Oftentimes, there is confusion between glob patterns and regular expressions. As we already mentioned before, glob patterns use some of the same characters that are used in regular expressions. Let's make it clear what each is used for, and what the difference between the two is.
In short, globbing is about file names. Regular expressions are about texts like written programs or parameters of grep command. Also, regular expressions are usually more sophisticated and complex than globbing. They use many more characters and that's why they have more options and features.
Some symbols have the same meaning in globbing and regular expressions. These are symbols like curly brackets {} and square brackets []. However, be careful with asterisks * and question marks ?. They have different purposes in glob patterns and regular expressions.
As you might already know, * in glob patterns means either zero or any number of characters. But in regex, * means zero or more of the previous character; that is, the character before *.
? in glob means any one character. In regex, it means zero or only one of the previous characters; that is, the character before ?.
. in regex means the same as what ? means in globbing, and a combination of .* in regex means the same as * in glob patterns means. Conclusion
Globbing is not that hard, and it is certainly easier than regular expressions. The major points that we discussed are:
- globbing is for file and directory names;
- most commonly used wildcards in globbing are
* ? ! ^ {} []; - symbols
*,?, and.mean different things in globbing and in regular expressions.
Now let's move on to practice glob patterns.