管道后如何使用反转grep?

时间:2015-11-11 15:56:03

标签: bash grep invert

我想列出文件服务器中的目录磁盘使用情况。 我也想忽略错误消息。这是我的命令:

load handel;
% y = data
% Fs = frequency = number of datapoints per second
% so calculate begin and end time using the sampling frequency:
totalTime = size(y,1)/Fs;  % all data divided by sampling frequency
beginTime = round(5*Fs);   % 5 seconds
endTime = round(7*Fs);     % 7 seconds

% playing full audio:
% player = audioplayer(y, Fs);
% play(player);

% playing only part of audio:
player = audioplayer(y(beginTime:endTime,:), Fs);
play(player);

结果仍然包含权限被拒绝的行:

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

我做错了什么?

1 个答案:

答案 0 :(得分:5)

这是因为“Permission denied”是通过标准错误发送的,而不是通过标准输出发送的。

如果您不想要这些信息,只需将stderr重定向到/ dev / null即可将其静音:

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

所有这些错误消息都会发生这种情况:

$ 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