Merge pull request #24699 from wxy/20220209-6-Linux-metacharacters-I-love-to-use-on-the-command-line

ALL:wxy:20220209-6-Linux-metacharacters-I-love-to-use-on-the-command-line
This commit is contained in:
Xingyu.Wang 2022-02-14 00:14:39 +08:00 committed by GitHub
commit c06a5a0d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 124 deletions

View File

@ -0,0 +1,104 @@
[#]: subject: "6 Linux metacharacters I love to use on the command line"
[#]: via: "https://opensource.com/article/22/2/metacharacters-linux"
[#]: author: "Don Watkins https://opensource.com/users/don-watkins"
[#]: collector: "lujun9972"
[#]: translator: "wxy"
[#]: reviewer: "wxy"
[#]: publisher: "wxy"
[#]: url: "https://linux.cn/article-14270-1.html"
我喜欢在 Linux 命令行中使用的 6 个元字符
======
> 在 Linux 命令行上使用元字符是提高生产力的一个好方法。
![](https://img.linux.net.cn/data/attachment/album/202202/13/235915kbphrm6ld6yi2hud.jpg)
在我的 Linux 之旅的早期,我学会了如何使用命令行。这就是 Linux 的与众不同之处。我可以失去图形用户界面GUI但没有必要完全重建机器。许多 Linux 电脑是<ruby>无头<rt>headless</rt></ruby>运行的,你可以在命令行上完成所有的管理任务。它使用许多所有人都熟悉的基本命令,如 `ls`、`ls-l`、`ls-l`、`cd`、`pwd`、`top` 等等。
### Linux 上的 Shell 元字符
你可以通过使用元字符来扩展这些命令。我不知道你怎么称呼它们,但这些元字符使我的生活变得更轻松。
### 管道符 |
假设我想知道我的系统上运行的 Firefox 的所有实例。我可以使用带有 `-ef` 参数的 `ps` 命令来列出我系统上运行的所有程序实例。现在我想只看那些涉及 Firefox 的实例。我使用了我最喜欢的元字符之一,管道符 `|`,将其结果送到 `grep`,用它来搜索模式:
```
$ ps -ef | grep firefox
```
### 输出重定向 >
另一个我最喜欢的元字符是输出重定向 `>`。我用它来打印 `dmesg` 命令结果中所有 AMD 相关的结果。你可能会发现这在硬件故障排除中很有帮助:
```
$ 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).
```
### 星号 *
星号 `*`(通配符)是寻找具有相同扩展名的文件时我的最爱,如 `.jpg``.png`。我首先进入我的系统中的 `Picture` 目录,并使用类似以下的命令:
```
$ ls *.png
BlountScreenPicture.png
DisplaySettings.png
EbookStats.png
StrategicPlanMenu.png
Screenshot from 01-24 19-35-05.png
```
### 波浪号 ~
波浪号 `~` 是在 Linux 系统上通过输入以下命令快速返回你的家目录的一种方法:
```
$ cd ~
$ pwd
/home/don
```
### 美元符号 $
`$` 符号作为一个元字符有不同的含义。当用于匹配模式时,它意味着任何以给定字符串结尾的字符串。例如,当同时使用元字符 `|``$` 时:
```
$ ls | grep png$
BlountScreenPicture.png
DisplaySettings.png
EbookStats.png
StrategicPlanMenu.png
Screenshot from 01-24 19-35-05.png
```
### 上尖号 ^
符号 `^` 将结果限制在以给定字符串开始的项目上。例如,当同时使用元字符 `|``^` 时:
```
$ ls | grep ^Screen
Screenshot from 01-24 19-35-05.png
```
这些元字符中有许多是通往 [正则表达式][2] 的大门,所以还有很多东西可以探索。你最喜欢的 Linux 元字符是什么,它们是如何节省你的工作的?
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/2/metacharacters-linux
作者:[Don Watkins][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/don-watkins
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/terminal_command_linux_desktop_code.jpg?itok=p5sQ6ODE (Terminal command prompt on orange background)
[2]: https://opensource.com/article/18/5/getting-started-regular-expressions

View File

@ -1,124 +0,0 @@
[#]: subject: "6 Linux metacharacters I love to use on the command line"
[#]: via: "https://opensource.com/article/22/2/metacharacters-linux"
[#]: author: "Don Watkins https://opensource.com/users/don-watkins"
[#]: collector: "lujun9972"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
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.
![Terminal command prompt on orange background][1]
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 &gt;
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 &gt; 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
```
### Carat ^
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][2], so there's a lot more to explore. What are your favorite Linux metacharacters, and how are they saving your work?
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/2/metacharacters-linux
作者:[Don Watkins][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/don-watkins
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/terminal_command_linux_desktop_code.jpg?itok=p5sQ6ODE (Terminal command prompt on orange background)
[2]: https://opensource.com/article/18/5/getting-started-regular-expressions