What Are Linux Metacharacters And How Do You Use Them

Learn how Linux metacharacters enhance your use of command line tools

Young computer science student developing with his computer on a Linux System over double screen system

Esteban Martinena Guerrero/Getty Images

According to Wikipedia, a metacharacter is any character that has a special meaning, such as a carat (^), dollar sign ($) or an asterisk (*).

In terms of Linux, there are a fair number of these metacharacters and their meanings differ depending on which Linux command or program you're running.

The Full Stop As A Metacharacter (.)

The humble full stop is used to indicate the current position when running commands such as cd, find or sh, but within applications such as awk, grep and sed it's a wildcard used to denote 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:

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

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:

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

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.

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:

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:

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.

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:

This lists all files that end with png.

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

For example:

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:

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

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

Brackets As A Metacharacter ([])

You can use brackets when searching for matterns. Brackets let you specify specific letters that you want to match anywhere in the pattern.

For example:

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

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.

For example:

This will run the which jpg command first, to find which directory jpg files are in, and the results of that search will get passed to the ls command, which will list all of those files.

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