我正在使用以下命令删除文件夹中的四个最大文件:
find "/var/www/site1/" -maxdepth 1 -type f | xargs ls -1S | head -n 4 | xargs -d '\n' rm -f
Run Code Online (Sandbox Code Playgroud)
它工作正常,但不时抛出破裂的管道错误:
xargs: ls: terminated by signal 13
Run Code Online (Sandbox Code Playgroud)
pla*_*ker 27
我遇到了类似的问题,发现这个帖子正在寻找答案:
信号13表示将某些内容写入管道,不再读取任何内容(例如,请参阅http://people.cs.pitt.edu/~alanjawi/cs449/code/shell/UnixSignals.htm).
这里的要点是,当下面的head命令已经获得了它想要的所有输入并关闭了它的输入管道时,由xargs执行的ls命令仍在写入输出.因此,忽略它是安全的,但它很难看.另请参阅https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error中接受的答案
您故意使用终止程序head -n 4,该程序会创建中断的管道,因为您是在“调用者”完成之前终止了它。由于这是您所期望的,因此可以通过将其重定向到/dev/null并丢弃该错误来忽略该错误:
find "/var/www/site1/" -maxdepth 1 -type f | xargs ls -1S | head -n 4
| xargs -d '\n' rm -f 2>/dev/null