6 Linux metacharacters I love to use on the command line

Using metacharacters on the Linux command line is a great way to enhance productivity.
Register or Login to like

Early in my Linux journey, I learned how to use the command line. It's what sets Linux apart. I could lose the graphical user interface (GUI), but it was unnecessary to rebuild the machine completely. Many Linux computers run headless, and you can accomplish all the administrative tasks on the command line. It uses many basic commands that all are familiar with—like ls, ls-l, ls-l, cd, pwd, top, and many more.

Shell metacharacters on Linux

You can extend each of those commands through the use of metacharacters. I didn't know what you called them, but metacharacters have made my life easier.

Pipe |

Say that I want to know all the instances of Firefox running on my system. I can use the ps command with an -ef to list all instances of the programs running on my system. Now I'd like to see just those instances where Firefox is involved. I use one of my favorite metacharacters, the pipe | the result to grep, which searches for patterns. 

$ ps -ef | grep firefox 

Output redirection >

Another favorite metacharacter is the output redirection >. I use it to print the results of all the instances that Intel mentioned as a result of a dmesg command. You may find this helpful in hardware troubleshooting. 

$ dmesg | grep amd > amd.txt
$ cat amd.txt
[ 0.897] amd_uncore: 4 amd_df counters detected
[ 0.897] amd_uncore: 6 amd_l3 counters detected
[ 0.898] perf/amd_iommu: Detected AMD IOMMU #0 (2 banks, 4 counters/bank).

Asterisk *

The asterisk * or wildcard is a favorite when looking for files with the same extension—like .jpg or .png. I first change into the Picture directory on my system and use a command like the following: 

$ ls *.png
BlountScreenPicture.png
DisplaySettings.png
EbookStats.png
StrategicPlanMenu.png
Screenshot from 01-24 19-35-05.png

Tilde ~

The tilde ~ is a quick way to get back to your home directory on a Linux system by entering the following command: 

$ cd ~
$ pwd
/home/don

Dollar symbol $

The $ symbol as a metacharacter has different meanings. When used to match patterns, it means any string that ends with a given string. For example, when using both metacharacters | and $

$ ls | grep png$
BlountScreenPicture.png
DisplaySettings.png
EbookStats.png
StrategicPlanMenu.png
Screenshot from 01-24 19-35-05.png

Caret ^

The ^ symbol restricts results to items that start with a given string. For example, when using both metacharacters | and ^

$ ls | grep ^Screen
Screenshot from 01-24 19-35-05.png

Many of these metacharacters are a gateway to regular expressions, so there's a lot more to explore. What are your favorite Linux metacharacters, and how are they saving your work?

User profile image.
Educator, entrepreneur, open source advocate, life long learner, Python teacher. M.A. in Educational Psychology, MSED in Educational Leadership, Linux system administrator, Follow me at @Don_Watkins . Mastodon ID

11 Comments

The "caret" is not even a metacharacter of the command line shell. It is a metacharacter used in programs you run from the shell, that use the given parameters as a regular expression.
This is not the same for "*" which is intepreted by the shell "patterns" (not regexps).

There's are more useful shell metacharacters:
- dollar ($) for the value of environment variables
- backquotes (`) in pairs for inserting the output of a subcommand into the command line of another command: this is my favorite just after the I/O redirectors ("|", "<", ">") as it captures an ouput only in memory without needing any named variable.

In reply to by Scott Nesbitt

You can actually change to your home directory by just entering "cd" and pressing enter. You don't need the tilde unless you are referring to a subdirectory, like: "cd ~/.ssh"

The tilde ~ by itself expands to your home directory. Also worthy of note is that you can refer to someone else's home directory by prefixing their login name with ~

Eg
cd ~/dave will look for subdir "dave" under your home directory

But
cd ~dave will look for user dave's home directory.

And bash's <tab> completion works with this,
eg
cd ~grah<tab> will expand user names from /etc/passwd that begin with "grah".

The caret is my new favorite metachacter, at least with bash. It simply repeats the last command making a substitution. Here are a couple of examples.

$ unzip -l somefile.zip
$ ^-l^

$ beet ls -a some album
$ ^ls^rm -df

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.