A metacharacter is any character that has a special meaning, such as a carat (^), a dollar sign ($), or an asterisk . Linux has a fair number of these metacharacters, and their meanings differ depending on which Linux command
you are using.
Full Stop as Metacharacter (.) Full Stop ( . ) Indicates the current position when running commands such as cd , find or sh. In applications such as awk , grep and sed
is a wildcard that represents a specific number of any character. For example, the following command finds everything MP3 files
находить . -тип f -name '* .mp3'
in the current folder and its subfolders.
If you run this command in the current working directory (pwd), you will see the returned results, assuming you store the MP3 files in the music folder in your home folder.
ps -ef | grep f..efox
Now look at this command: The ps command lists all running processes on your computer.
linux dot The ps -ef command returns a list of running processes. metacharacter pipe( | ) sends this list to the grep command, which looks for any line in the list containing f..efox
where dots refer to any two characters. If Firefox is running, you will get a match.
Likewise, if a program named fonefox or freefox is running, these are also returned. If you only need to look for a single character, instead of a dot (.) metacharacter, use ? metacharacter. Using ?
A metacharacter refers to any single character either at the beginning or at the end of a pattern.
Asterisk as a metacharacter Asterisk ( * ) is a well-known metacharacter.
ls * .flac
List of asterisks in Linux List of asterisks in Linux Part *.flac
ps -ef | grep f * efox
command returns a match for any filename ending in .flac.
Asterisk symbol in Linux
Asterisk symbol in Linux Carat as a wildcard (^) Carat ( ^
) is used to mark the beginning of a line or string.
So how is it used? The ls command lists the files in a folder like this:
ls -a | grep ^ gnome
If you want to list files in a folder that starts with a specific string, like gnome, carat can be used to specify that string. For example:
character in carats character in carats
In the example above, the ls command returns a list of filenames and passes this list to the grep command, which is used for pattern matching.
Grep knows that the character karat means find anything that starts with the characters following it, and in this case it is gnome. Dollar symbol as metacharacter ($) Dollar symbol ( $ ) has multiple meanings as a metacharacter in Linux.
ls | grep png $
When used for pattern matching, it means the opposite of carat and denotes any pattern that ends with a specific string.
For example: This list contains all files ending with png.
#! / bin / bash
export dog = molly
echo $ dog
Linux dollar sign Linux dollar sign Line export dog = molly creates an environment variable named dog and sets its value molly . To access an environment variable, use the $ symbol. With symbol $ operator echo $dog displays Molly. Without this in expression
the word dog is displayed. Escaping metacharacters (\\) Sometimes you don’t want a metacharacter to have a special meaning. For example, if the file is named f.refox, and the other file is called
ls | grep f.refox
. Now look at the following command:
Linux escape character
ls | grep f \\. refox
Linux escape characterTo return just f.refox, escape the full stop to actually mean the full stop like this:
Parentheses as a metacharacter (  ) You can use brackets ( ) when looking for patterns.