What Are Linux Metacharacters and How Do You Use Them?

Learn how Linux metacharacters enhance your use of command line tools

A metacharacter is any character that has a special meaning, such as a carat (^), dollar sign ($), or an asterisk (*). Linux has a fair number of these metacharacters, and their meanings differ depending on which Linux command or program you use.

The Full Stop as a Metacharacter (.)

The Full Stop (.) indicates the current position when running commands such as cd, find, or sh. In applications such as awk, grep, and sed, it's a wildcard that denotes a specific number of any character.

As an example, the following command finds all MP3 files in the current folder and its subfolders.

find . -type f -name '*.mp3'

If you run that command in the present working directory (pwd), you'll see results returned, assuming you keep MP3 files in a music folder in your home folder.

Now look at this command:

ps -ef | grep f..efox

The ps command lists all the running processes on your computer. The grep command takes lines of input and searches for a pattern.

Linux dot character

The ps -ef command retrieves a list of running processes. The pipe (|) metacharacter sends that list to the grep command, which searches for any line in the list that contains f..efox, where the periods refers to two of any character.

If Firefox is running, you'll get a match. Similarly, if a program called fonefox or freefox is running, these are also returned.

If you only need to search for a single character, instead of using the full stop (.) metacharacter, use the ? metacharacter. Using the ? metacharacter refers to any single character either at the beginning or at the end of the pattern.

The Asterisk as a Metacharacter (*)

The asterisk (*) is a universally known metacharacter. It means zero or more of any character when searching for a pattern. For example:

ls *.flac
Linux asterisk listing

The *.flac portion of the command returns a match for any filename that ends in .flac. Similarly, you can use the asterisk with the grep command in the last section as follows:

ps -ef | grep f*efox

This differs slightly because the asterisk means zero or more, so as well as finding firefox, facefox, and fonefox, it also finds flutefox, ferretfox, and fefox.

Linux asterisk character

The Carat as a Metacharacter (^)

The carat (^) is used to denote the start of a line or a string. So how is it used?

The ls command lists the files in a folder, as follows:

ls -a

If you want to list the files in a folder that begin with a certain string, for example, gnome, the carat can be used to specify that string. For example:

ls -a | grep ^gnome

This lists the files that start with gnome. If you want files that have gnome anywhere in the filename, use the asterisk.

Linux carat character

In the above example, the ls command returns a list of filenames and passes that list to the grep command, which is used for pattern matching. Grep knows that the carat symbol means to find anything that starts with the characters that come after it, and in this case, that's gnome.

The Dollar Symbol as a Metacharacter ($)

The dollar symbol ($) has multiple meanings as a metacharacter in Linux. When used to match patterns, it means the opposite of carat and denotes any pattern that ends with a particular string. For example:

ls | grep png$

This lists all files that end with png.

The dollar symbol is also used to access environment variables within the bash shell. For example:

#! /bin/bash
export dog=molly
echo $dog
Linux dollar sign character

The line export dog=molly creates an environment variable called dog and sets its value molly. To access the environment variable, use the $ symbol. With the $ symbol, the echo $dog statement displays molly. Without it, the echo dog statement displays the word dog.

Escaping Metacharacters (\\)

Sometimes you don't want the metacharacter to have a special meaning. For example, if a file is called f.refox and another file is called firefox.

Now look at the following command:

ls | grep f.refox

What do you think is returned? Both f.refox and firefox are returned because both match the pattern.

Linux escape character

To only return f.refox, escape the full stop to actually mean a full stop, as follows:

ls | grep f\\.refox

Brackets as a Metacharacter ([])

You can use brackets ([ ])when searching for patterns. Brackets specify specific letters to match anywhere in the pattern. For example:

ls | grep [abc]

This lists all files that contain the letters a, b, or c.

Linux bracket character

You can use brackets at the beginning, end, or in the middle of a search pattern.

To search for a range of letters, use the hyphen (-) character. For example, ls [a-h]* returns files that start with any letter from a through h.

The Accent Grave Metacharacter

In the examples above, the pipe metacharacter sends the results of one command (like the ls command) to another command (like the grep command).

An alternative way to do this is to use the back quote, also known as the accent grave (`), to insert the results of one command into another command. To do this, store the result of one command in a variable. For example:

command=`ls -lah`
echo $command
Linux backtick character

The example is super contrived, and you'll likely never do anything like that, but you can see how it works. The backtick character runs a command and saves the result. This can be useful in scripting, but it's less flexible than using the pipe for simple commands.

Common Metacharacters and Their Meanings

Character Meaning
. Any character.
* Zero or more characters.
^ Match any line or string that starts with a pattern (for example, ^gnome).
$ Match any line or string that ends with a pattern (for example, gnome$).
\ Escapes the next character to remove its special meaning.
[] Match one of a list or range (for example, ["abc", "def"] or [1..9]).
+ Match one or more preceding (for example, grep a+).
? Match zero or one preceding.