Home >System Tutorial >LINUX >6 Linux metacharacters I love using on the command line
Early in my Linux journey, I learned how to use the command line. This is what makes Linux unique. I might lose the graphical user interface (GUI), but there's no need to completely rebuild the machine. Many Linux computers run in headless mode, allowing you to complete all administrative 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
, etc.
You can expand each of these commands by using metacharacters. I don't know what you call them, but using metacharacters on the Linux command line is a great way to increase productivity.
Suppose I want to know all instances of Firefox running on my system. I can use the ps
command with -ef
to list all instances of a program running on my system. Now I want to look at those instances involving Firefox. I use one of my favorite metacharacters, piping |
to the grep
program, which scans the entire file contents.
means
and grep
commands are executed at the same time<pre class="brush:js;toolbar:false;">linuxmi@linuxmi /home/linuxmi/www.linuxmi.com
⚡ ps -ef | grep firefox
</pre>
<h2 style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 22px;text-align: left;margin: 20px 10px 0px 0px"><span class="content" style="font-size: 18px;font-weight: bold;color: #222;display: inline-block;padding-left: 10px;border-left: 5px solid #f83929"><img src="https://img.php.cn/upload/article/000/887/227/170779863019344.png" alt="我喜欢在命令行中使用的 6 个 Linux 元字符"></span></h2>
<h2 style="margin-top: 30px;margin-bottom: 15px;padding: 0px;font-weight: bold;color: black;font-size: 22px;text-align: left;margin: 20px 10px 0px 0px"><span class="content" style="font-size: 18px;font-weight: bold;color: #222;display: inline-block;padding-left: 10px;border-left: 5px solid #f83929">Output Redirection ></span></h2>
<p>Another favorite metacharacter is output redirection <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin: 0 2px;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;color: #10f5d6c">>
. I use it to display dmesg messages for the eth0 user interface using the grep option followed by eth0. The command results are saved to linuxmi.txt
. You may find this useful.
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com ⚡ sudo dmesg | grep -i eth0 > linuxmi.txt [sudo] linuxmi 的密码: linuxmi@linuxmi /home/linuxmi/www.linuxmi.com ⚡ cat linuxmi.txt [ 3.647685] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:0e:a2:7c [ 3.647694] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection [ 3.651280] e1000 0000:02:01.0 ens33: renamed from eth0
The asterisk *
or wildcard characters are preferred when looking for files with the same extension (such as .jpg
or .png
). I first enter the image directory in the system, and then use the following command:
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com ⚡ ls *.png linuxmi.com.png www.linuxmi.com.png
The tilde ~
is a quick way to return to the home directory of your Linux system by entering the following command:
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com ⚡ cd ~ linuxmi@linuxmi /home/linuxmi ⚡ pwd /home/linuxmi
The $
symbol as a metacharacter has a different meaning. When used to match a pattern, it represents any string that ends with the given string. For example, when using the metacharacters |
and $
together:
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com ⚡ ls | grep png$
^
符号将结果限制为以给定字符串开头的项。例如,当同时使用元字符 |
和^
时:
linuxmi@linuxmi /home/linuxmi/www.linuxmi.com ⚡ ls | grep ^linuxmi
其中许多元字符是通向正则表达式的入口,因此还有很多东西需要探索。请问下你最熟悉的 Linux 元字符是什么,它们是如何拯救你的工作的?
相关文章:
The above is the detailed content of 6 Linux metacharacters I love using on the command line. For more information, please follow other related articles on the PHP Chinese website!