Unix查找命令忽略丢失的文件或目录

时间:2018-05-12 07:03:04

标签: bash macos unix terminal

我有这个小命令删除~/Library/Cache~/Library/Logs/Library/Cache/Library/Logs目录中的所有文件,但有时会丢失一个或多个目录,{{ 1}}命令不执行。

rm -rf

我希望命令忽略丢失的目录,只需对找到的文件执行命令。

1 个答案:

答案 0 :(得分:0)

这里唯一的问题是你看到有关丢失目录的错误消息而感到恼火。

您可以将标准错误流重定向到/dev/null以忽略错误:

sudo find ~/Library/Caches ~/Library/Logs \
           /Library/Caches  /Library/Logs \
           -mindepth 1 -type f -exec rm -rf {} + 2>/dev/null

另请注意,此处不需要-mindepth 1,并且某些find实施具有-delete

sudo find ~/Library/Caches ~/Library/Logs \
           /Library/Caches  /Library/Logs \
           -type f -delete 2>/dev/null

或者,使用了解大括号扩展的shell:

sudo find {~,}/Library/{Logs,Caches} -type f -delete 2>/dev/null