What Are Linux Metacharacters And How Do You Use Them

Linux Metacharacters
Linux Metacharacters.

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 command or program you are running.

The Full Stop As A Metacharacter (.)

The humble full stop is used to donate the current position when running commands such as cd, find or sh but within applications such as awk, grep and sed it is used to denote any character.

As an example, the following command will find all mp3 files in the current folder and below.

find . -name *.mp3

If you run that command in you present working directory (pwd) then you will probably get 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.

Therefore the ps -ef command gets a list of running processes and gives it to grep which searches for any line in the list that has f..efox where the . can mean any character.

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

The asterisk as a metacharacter (*)

The asterisk is a more universally well-known metacharacter and is used to mean 0 or more when searching for a pattern.

For example:

find . -name *.mp3

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

ps -ef | grep F*efox

It is 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:

ls | 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 again revert back to the asterisk.

In the above example, the ls returns a list of filenames and passes that list to grep 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, it is a 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:

export dog=molly
echo $dog

The line export dog=molly creates an environment variable called dog and sets its value to molly. To access the environment variable the $ symbol is used. With the $ symbol the echo $dog statement displays molly but 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. What 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.

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

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