linux - 使用 Bash 重定向管道中的错误时遇到问题?

标签 linux bash ls

ls -lhR /etc/ | egrep *.conf$ >/home/student/total_size.txt 2>/home/student/error.txt

因此我使用此命令从 /etc/ 获取所有 .conf 文件。我想要 total_size.txt 中的输出和 error.txt 中的错误。我的输出看起来不错,但错误不会重定向到 error.txt,它们出现在我的终端中:

ls: cannot open directory '/etc/cups/ssl': Permission denied

我不知道该怎么办;我尝试使用 2>> 而不是 2> 但它也不起作用。

最佳答案

发生这种情况是因为 lsstderr 仍然指向终端。您需要将管道括在花括号中,并在外部进行重定向。例如:

{ ls -lhR /etc/ | egrep *.conf$; } >/home/student/total_size.txt 2>/home/student/error.txt

关于linux - 使用 Bash 重定向管道中的错误时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58997776/

相关文章:

linux - 使用 bash 从文本中获取特定字符串

linux - 确定 linux 端口是否支持硬件中的 cmpxchg

c - 为什么在 Vim 中运行 C 代码会跳过 scanf()?

Python:无法访问新定义的环境变量

ios - 关于应用程序的内存布局和一些变量的逻辑地址

bash - 修复小型 Bash 程序中的 POSIX sh 警告

bash - 如何简单地在 bash 中计算两个变量的最小值?

linux - 什么是 Linux 中的长格式?

linux - 如何在 Linux 中列出目录中的文件和文件夹及其总大小?

bash - 获取按日期分组的文件列表