The metacharacters are helpful in listing, removing, and copying files on Linux. However, the function of each metacharacter differs depending on the command you are using it with.

This article provides an in-depth guide on different types of metacharacters in Linux. Lastly, we explain how these special characters help in connecting and expanding commands.

File Matching Metacharacters

The Linux shell allows you to save keystrokes while typing commands by using metacharacters between files or directory names. These characters help you refer to a group of files or a directory to list, move or perform other activitities on.

These are some file-matching metacharacters that the Linux shell can interpret:

  • (Asterisk): Matches single or multiple occurrences of a character ? (Question mark): Matches a single character or a pattern occurrence (Square Brackets): Matches any hyphen-separated number, symbol, or alphabets specified inside the squared brackets

An ideal way to practice metacharacters in Linux is by creating a new empty folder inside the /tmp directory.

Now navigate into the /tmp/meta directory using the cd command and create new empty files using touch, as follows:

Use the following commands to test the “*” metacharacter and display the outputs:

The aforementioned command will delete all the files containing the letter “p” in its name. You can verify the change using the ls command as follows:

Here are some examples of the “?” metacharacter for pattern matching:

The last command matches any file that begins with c and has t as the third letter (cat.txt, catfish.sh, etc.).

Now use the [av]* option with the ls command to list all files that begin with either a or v, as follows:

You can modify the above command to only list files that end with the letter t:

Similarly, you can use the hyphen separated letters to define ranges and list files as follows:

File Redirection Metacharacters

For a better understanding of redirection in Bash, each process in Linux has file descriptors, known as standard input (stdin/0), standard output (stdout/1), and standard error (stderr/2). They determine the origin of the command input and decide where to send the output and error messages.

The redirection metacharacters help you modify these actions by redirecting the content I/O flow. Generally, the Linux shell reads the command input from the keyboard and writes the output to the screen. The input redirection allows the command to read the content from a file instead of a keyboard, while output redirection saves the command output to a file.

In other words, the Linux file redirection metacharacters allow you to redirect the content to (>) and from (<) the files. The three primary redirection metacharacters are:

<: Directs the file content to the command. For instance, the command output for less . bashrc is the same as less < . bashrc.

: Directs the command output to the file. The command ls /etc > lists. txt saves the output to the lists. txt file.

: Appends the command output to the file content.

wc stands for word count and you can use it to display the difference between the file before and after appending it with the output.

Brace Expansion Metacharacter

The brace expansion metacharacter allows you to expand the characters across directories, file names, or other command-line arguments. For instance, you can make a new directory brace inside the /tmp folder and create a set of files using the touch command as follows:

Now, you can check if touch created the files or not using the ls command.

You can specify multiple lists to generate file names based on the combinations of the elements in the list. For example:

The last command will create the following files in the current directory:

The first command uses two sets of braces to associate filenames in each set with the other. You can also write the last command as touch {a..c}.{1..3} to specify the range between a and c and 1 and 3.

In addition to creating files, you can also use brace expansion to remove or copy files to other locations.

Some Other Linux Metacharacters

Here is a table of some must known metacharacters for command connection and expansion with their names, description, and examples to practice:

Save Your Keystrokes With Linux Metacharacters

Linux metacharacters are also known as wildcards that add special meaning to the commands and control their behavior. Metacharacters optimize a user’s work performance in a productive environment while working around files/directories and connecting/expanding the Linux shell commands.

Besides, metacharacters are also the building blocks of regular expressions. Also, learning about metacharacters and their usage is an important skill to have if you want to become a pro-Linux user.