What Are Linux Metacharacters? Everything You Need to Know

What Are Linux Metacharacters? Everything You Need to Know

The most powerful feature of the Linux Bash shell is its capability to work around files and redirect their input and output efficiently. Linux uses special characters or symbols known as metacharacters that add special meaning to a shell command with respect to file search and commands connection.

The metacharacters are helpful in listing, removing, and copying files on Linux. However, the function of each metacharacter differs depending on the command you are using it with.

This article provides an in-depth guide on different types of metacharacters in Linux. Lastly, we explain how these special characters help in connecting and expanding commands.

The Linux shell allows you to save keystrokes while typing commands by using metacharacters between files or directory names. These characters help you refer to a group of files or a directory to list, move or perform other activitities on.

These are some file-matching metacharacters that the Linux shell can interpret:

  • * (Asterisk): Matches single or multiple occurrences of a character
  • ? (Question mark): Matches a single character or a pattern occurrence
  • [ ] (Square Brackets): Matches any hyphen-separated number, symbol, or alphabets specified inside the squared brackets

An ideal way to practice metacharacters in Linux is by creating a new empty folder inside the /tmp directory.

sudo mkdir /tmp/meta

Now navigate into the /tmp/meta directory using the cd command and create new empty files using touch, as follows:

touch apple.txt cider.sh vinegar.php cat.txt lemon.txt juice.sh catfish.sh 

Use the following commands to test the “*” metacharacter and display the outputs:

ls c*
catfish.sh cat.txt cider.sh
ls c*h
catfish.sh cider.sh
ls *r*
cider.sh vinegar.php
sudo rm *p*

The aforementioned command will delete all the files containing the letter “p” in its name. You can verify the change using the ls command as follows:

catfish.sh cat.txt cider.sh juice.sh lemon.txt

Here are some examples of the “?” metacharacter for pattern matching:

ls a?*
ls c?t*
catfish.sh cat.txt

The last command matches any file that begins with c and has t as the third letter (cat.txt, catfish.sh, etc.).

Now use the [av]* option with the ls command to list all files that begin with either a or v, as follows:

ls [av]*
apple.txt vinegar.sh

You can modify the above command to only list files that end with the letter t:

ls [ac]*[t]
apple.txt catfish.txt cat.txt

Similarly, you can use the hyphen separated letters to define ranges and list files as follows:

ls [a-j]*
apple.txt catfish.sh cat.txt cider.sh juice.sh

For a better understanding of redirection in Bash, each process in Linux has file descriptors, known as standard input (stdin/0), standard output (stdout/1), and standard error (stderr/2). They determine the origin of the command input and decide where to send the output and error messages.

The redirection metacharacters help you modify these actions by redirecting the content I/O flow. Generally, the Linux shell reads the command input from the keyboard and writes the output to the screen. The input redirection allows the command to read the content from a file instead of a keyboard, while output redirection saves the command output to a file.

In other words, the Linux file redirection metacharacters allow you to redirect the content to (>) and from (<) the files. The three primary redirection metacharacters are:

  1. <: Directs the file content to the command. For instance, the command output for less .bashrc is the same as less < .bashrc.
  2. >: Directs the command output to the file. The command ls /etc > lists.txt saves the output to the lists.txt file.
  3. >>: Appends the command output to the file content.

Append File With Redirection

wc stands for word count and you can use it to display the difference between the file before and after appending it with the output.

Related: Understanding Standard I/O on Linux

The brace expansion metacharacter allows you to expand the characters across directories, file names, or other command-line arguments. For instance, you can make a new directory brace inside the /tmp folder and create a set of files using the touch command as follows:

sudo mkdir /tmp/brace; cd /tmp/brace
touch test{1,2,3,4,5}

Now, you can check if touch created the files or not using the ls command.

test1 test2 test3 test4 test5

You can specify multiple lists to generate file names based on the combinations of the elements in the list. For example:

touch {apple,cider,vinegar}.{fruit,liquid,sour}
touch {a,b,c}.{1,2,3}

The last command will create the following files in the current directory:

creating new files with touch

The first command uses two sets of braces to associate filenames in each set with the other. You can also write the last command as touch {a..c}.{1..3} to specify the range between a and c and 1 and 3.

In addition to creating files, you can also use brace expansion to remove or copy files to other locations.

Here is a table of some must known metacharacters for command connection and expansion with their names, description, and examples to practice:

Name Description Example
Pipe (|) Connects command output as an input to the other command. cat /etc/passwd | grep root
Semicolon (;) Allows execution of sequential commands, one after the other. cd /etc ; ls -la ; chmod +x /tmp/script.php
Ampersand (&) Runs the processes or commands in the background. find / -perm -u=s -type f &
Dollar ($) Expands the arithmetic expression and passes it to the shell echo “total files in this directory are: $(ls | wc -l)”
Null Redirection (2>) Directs standard error messages to the /dev/null file your_command 2>/dev/null
Circumflex (^) Matches any pattern that begins with the expression followed by ^ cd /etc/ssh ; ls | grep ^s

Linux metacharacters are also known as wildcards that add special meaning to the commands and control their behavior. Metacharacters optimize a user’s work performance in a productive environment while working around files/directories and connecting/expanding the Linux shell commands.

Besides, metacharacters are also the building blocks of regular expressions. Also, learning about metacharacters and their usage is an important skill to have if you want to become a pro-Linux user.

The Beginner’s Guide to Regular Expressions With Python

Want to speed up your Python workflow with a few simple commands? Regular expressions are your friend.

Read Next

About The Author

Original Link

Leave a Comment

Your email address will not be published.

Scroll to Top