我无法重定向错误(以bash表示)

问题描述 投票:2回答:2
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>,但它也不起作用。

linux bash ls
2个回答
2
投票

之所以会这样,是因为lsstderr仍指向终端。您需要将管道用大括号括起来,然后在外部进行重定向。例如:

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

1
投票

尝试一下,应该解决这个问题。

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

错误是由ls而不是egrep产生的。

© www.soinside.com 2019 - 2024. All rights reserved.