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 - 如何防止长时间运行的备份作业被杀死

c - 在 C 中实现 ls 命令

linux - 如何使用 xargs 将 ls 通过管道传输到 cat 中,以便列出文件名?

linux - Bash:检查命令是否返回特定值

c - 使用 gdb 调试 linux 内核模块

linux - nagios 为 Jenkins 创建自定义警报

用于 SSH SOCKS 绑定(bind)(动态端口转发)的 Java 库?

linux - 向shell中递归扩展的变量添加前缀

arrays - 将多个数组作为参数传递给 Bash 脚本?

bash - 如何删除 BASH 中只包含数字的行?