Users coming from graphical operating systems may ask why Linux system administrators love the command line so much. Here we see one aspect of the power of the Linux command line: wildcards or blobbing.
Some of the tasks performed on the command line seem to more easily done with a graphical file manager. With a file manager, we can drag and drop a file from one directory to another, cut and paste files, delete files, and so on. So why use these old command line programs?
The answer is power and flexibility. While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. For example, how could we copy all the MP3 files from one directory to another but copy only files that do not exist in the destination directory or are newer than the versions in the destination directory? It’s pretty hard with a file manager but pretty easy with the command line.
cp -u *.mp3 destination
This power comes from a Linux shell that matches patterns of stings to a group of files. Because the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are called wildcards. Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters. Table below lists the wildcards and what they select.
Wildcard |
Meaning |
* |
Matches any characters |
? |
Matches any single character |
[characters] |
Matches any character that is a member of the set characters |
[!characters] |
Matches any character that is not a member of the set characters |
[[:class:]] |
Matches any character that is a member of the specified class |
Commonly used character classes are shown in table below.
Character class |
Meaning |
[:alnum:] |
Matches any alphanumeric character |
[α |
Matches any alphabetic character |
[:digit:] |
Matches any numeral |
[:lower:] |
Matches any lowercase letter |
[:upper:] |
Matches any uppercase letter |
Using wildcards makes it possible to construct sophisticated selection criteria for filenames. Table below provides some examples of patterns and what they match.
Pattern |
Matches |
* |
All files |
g* |
Any file beginning with g |
b*.txt |
Any file beginning with b followed by any characters and ending with .txt |
Data??? |
Any file beginning with Data followed by exactly three characters |
[abc]* |
Any file beginning with either an a, a b, or a c |
BACKUP.[0-9][0-9][0-9] |
Any file beginning with BACKUP. followed by exactly three numerals |
[[:upper:]]* |
Any file beginning with an uppercase letter |
[![:digit:]]* |
Any file not beginning with a numeral |
*[[:lower:]123] |
Any file ending with a lowercase letter or the numerals 1, 2, or 3 |
Wildcards can be used with any command that accepts filenames as arguments. It is an essential part of command line interface and shell scripting and faniliarity with it is a basic need for any system administrator.