简体   繁体   English

管道后如何使用invert grep?

[英]How can use invert grep after pipe?

I want to list directory disk usage in a fileserver. 我想列出文件服务器中的目录磁盘使用情况。 I also want to ignore the error messages. 我也想忽略错误消息。 Here is my command: 这是我的命令:

du -sh * | grep -v "Permission denied" | sort -n

The result still contains the permission denied lines: 结果仍然包含权限被拒绝的行:

du: cannot access './myFile1/': Permission denied
du: cannot access './myFile2/': Permission denied
du: cannot access './myFile3/': Permission denied

What am I doing wrong? 我究竟做错了什么?

This is because the "Permission denied" is sent through standard error, not through standard output. 这是因为“权限被拒绝”是通过标准错误而不是通过标准输出发送的。

If you don't want this information, just silence it by redirecting stderr to /dev/null: 如果您不想要此信息,只需将stderr重定向到/ dev / null使其静音:

du -sh * 2>/dev/null | sort -n

This happens with all these error messages: 所有这些错误消息都会发生这种情况:

$ touch a
$ ls a asfasd
ls: cannot access asfasd: No such file or directory
a
$ ls a asfasd | grep cannot
ls: cannot access asfasd: No such file or directory
$ ls a asfasd 2>/dev/null  
a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM