Linux 中的管道和重定向
介绍
管道和重定向是 Linux 中使用的两种不同的机制。有时,我们需要将一个命令的输出作为另一个命令的输入并执行某些操作。在这种情况下,我们使用管道操作符。操作符是“|”。它位于“Enter”键的上方。有时,我们会将所有命令的输出重定向或传递到文件以进行存储。此外,我们将文件的内容作为命令的输入。这称为重定向,并使用“>”、“>>”和“<”等操作符。
在本文中,我们将学习如何在 Linux 中使用管道和重定向。
示例 1:重定向到新文件
当我们在 Linux 终端中运行任何命令时,输出将打印在屏幕上。如果我们想发送命令的输出,我们可以使用“>”来执行此操作。
在第一个示例中,我们将看到终端中的输出。
$ ls -lhrt total 16K drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cat-more-less drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cmd-bash
现在,在使用“>”之后,终端中将没有任何输出,输出将存储在“redirection.txt”中。
$ ls -lhrt > redirection.txt $ cat redirection.txt total 16K drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash -rw-rw-r-- 1 rian rian 0 Feb 11 22:43 redirection.txt drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cat-more-less drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cmd-bash
示例 2:重定向到现有文件
在最后一个示例中,我们已将输出重定向到新文件。我们也可以将输出重定向到现有文件。但是有一个问题。如果我们使用“>”重定向到现有文件,则现有文件的内容将消失。
$ cat 123.txt 123 $ ls -lhrt > 123.txt $ cat 123.txt total 20K drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash -rw-rw-r-- 1 rian rian 269 Feb 11 22:43 redirection.txt -rw-rw-r-- 1 rian rian 0 Feb 11 22:50 123.txt drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cat-more-less drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cmd-bash
现在我们可以看到文件 123.txt 的旧内容消失了。在这种情况下,我们必须使用“>>”来保留旧内容并追加重定向的内容。
$ cat 123.txt 123 $ ls -lhrt >> 123.txt $ cat 123.txt 123 total 24K drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash -rw-rw-r-- 1 rian rian 269 Feb 11 22:43 redirection.txt -rw-rw-r-- 1 rian rian 4 Feb 11 22:53 123.txt drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cat-more-less drwxrwxr-x 2 rian rian 4.0K Feb 11 2016 cmd-bash
示例 3:从现有文件重定向
我们还可以从现有文件重定向并为任何命令提供输入。让我们看看下面的示例。在这里,我们使用“grep”命令从文件“123.txt”的内容中搜索“123”。
$ grep -i "123" < 123.txt 123 -rw-rw-r-- 1 rian rian 4 Feb 11 22:53 123.txt
示例 4:重定向 STDERR
我们可以使用“>”之前的“2”将仅错误重定向到文件。
$ ls nofile.txt ls: cannot access 'nofile.txt': No such file or directory $ ls nofile.txt 2> error.txt $ cat error.txt ls: cannot access 'nofile.txt': No such file or directory
一个限制是,“error.txt”将仅包含 STDERR。如果我们希望在一个文件中获取 STDOUT 和 STDERR,则必须使用以下命令。让我们看看步骤。
$ ls nofile.txt bash/ ls: cannot access 'nofile.txt': No such file or directory bash/: bash_function.sh bash_pass_arg.sh bash_return.sh bash_return_usingglobal.sh return3.sh $ ls nofile.txt bash/ 2> error.txt bash/: bash_function.sh bash_pass_arg.sh bash_return.sh bash_return_usingglobal.sh return3.sh $ cat error.txt ls: cannot access 'nofile.txt': No such file or directory $ ls nofile.txt bash > error.txt 2>&1 $ cat error.txt ls: cannot access 'nofile.txt': No such file or directory bash: bash_function.sh bash_pass_arg.sh bash_return.sh bash_return_usingglobal.sh return3.sh
示例 1:使用单个和多个管道
我们可以将一个命令的输出传递到后续命令以执行某些操作并获得所需的输出。让我们看看下面的一个简单示例。
$ ls 123.txt bash cat-more-less cmd-bash error.txt redirection.txt zip-unzip $ ls | grep -i "bash" bash cmd-bash
我们可以看到命令从左到右流动。
可以在一行中使用多个管道来获取所需的输出。
$ ls | grep -i "bash" | wc -l 2
结论
通过本文,我们了解了如何在 Linux 中使用管道和重定向。
我们学习了以下操作符。
“>” , “>>”, “<”, “|”
这些命令有助于我们更快地操作并获得预期的输出。因此,根据需要,我们应该在 Linux 中使用相应的命令。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP