baicai

白菜

一个勤奋的代码搬运工!

6 Metacharacters Used in Linux Command Line

Using metacharacters in the Linux command line is a good way to improve productivity.

Many Linux computers are headless, and you can perform all management tasks on the command line. It uses many basic commands that everyone is familiar with, such as ls, ls-l, ls-l, cd, pwd, top, and so on.

Shell Metacharacters on Linux

You can expand these commands by using metacharacters.

Pipe Symbol |#

Suppose I want to know all instances of Firefox running on my system. I can use the ps command with the -ef parameter to list all program instances running on my system. Now I only want to see instances involving Firefox. I use one of my favorite metacharacters, the pipe symbol |, to send its result to grep and use it to search for patterns:

$ ps -ef | grep firefox

Output Redirection >#

Another favorite metacharacter of mine is output redirection >. I use it to print all AMD-related results in the output of the 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 * (wildcard) is my favorite when it comes to finding files with the same extension, such as .jpg or .png. I first enter the Picture directory in my system and use a command similar to 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 return to your home directory on a Linux system by entering the following command:

$ cd ~
$ pwd
/home/don

Dollar Sign $#

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

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

Caret ^#

The caret symbol ^ limits the results to items that start with the given string. For example, when using both the metacharacters | and ^ together:

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

Many of these metacharacters are gateways to regular expressions, so there is much more to explore. What are your favorite Linux metacharacters and how do they save you time at work?

via:
1
2

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.