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 will find all mp3 files in the current folder and its subfolders.

If you run that command in your present working directory (pwd), you'll probably see results returned, assuming you keep your mp3 files in a music folder within 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

Therefore 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 period refers to two of any character.

If you have firefox running, you'll get a match. Similarly, if you have a program called fonefox or freefox running, they'll also be returned.

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

The asterisk as a metacharacter (*)

The asterisk is a more universally known metacharacter, and is used to mean zero or more of any character when searching for a pattern.

For example:

ls *.flac
Linux asterisk listing

The *.flac returns a match for any filename that ends in .flac. Similarly, I could have used the asterisk with the grep command in the last section as follows:

ps -ef | grep f*efox

It's worth noting that this differs slightly because the asterisk means zero or more, so as well as finding firefox, facefox and fonefox it can also find flutefox, ferretfox and even just 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 is used to list all the files in a folder as follows:

ls -a

If you want to know all the files in a folder which begins with a certain string such as "gnome" then the carat can be used to specify that string.

For example:

ls -a | grep ^gnome

Note that this only lists files that start with gnome. If you want files that have a gnome in the filename anywhere then you would revert back to using the asterisk.

Linux carat character

In the above example, the ls 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 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 can have multiple meanings as a metacharacter within Linux.

When used to match patterns, it means the opposite to 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 the $ symbol is used. With the $ symbol, the echo $dog statement displays "molly". Without it, the echo dog statement just displays the word dog.

Escaping Metacharacters (\\)

Sometimes you don't want the metacharacter to have a special meaning. For example, consider if you have a file called f.refox and a file 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 they both match the pattern.

Linux escape character

To only return f.refox you would need to 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 let you specify specific letters that you want 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 even in the middle of a search pattern.

If you want to search for a large range of letters, you can instead use the hypen (-) character. For example ls [a-h]* will return all files that start with any letter from a through h.

The Accent Grave Metacharacter

In all of the examples above, the pipe metacharacter was used to send 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. You do this by storing 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'd likely never do anything like that, but you can clearly see how it works. The backtick character lets you run a command and save the result. This can be useful in scripting, but it's much less flexible than using the pipe for simple one-off commands.

Common Metacharacters And Their Meanings

Character Meaning
. Any character
* Zero or more characters
^ Match any line or string which start with a pattern (i.e ^gnome)
$ Match any line or string ending with a pattern (i.e gnome$)
\ Escapes the next character to remove its special meaning
[] Match one of a list or range (i.e ["abc", "def"] or [1..9]
+ Match one or more preceding (i.e grep a+)
? Match zero or one preceding
List Of Linux Metacharacters