Linux 中的管道和重定向Linux 中的管道和重定向Linux 中的管道和重定向Linux 中的管道和重定向
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

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”命令从dile“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 中的 PIPE 和 REDIRECTION。

我们已经学习了下面的运算符。

“>” , “>>”, “<”, “|”

这些命令帮助我们更快地进行操作并获得预期的输出。因此,我们应该根据需要在Linux中使用适当的命令。

©2015-2025 艾丽卡 support@alaica.com