简体   繁体   English

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

[英]Unix Find Command Ignoring Missing File or Directory

I have this little command that delete all files within ~/Library/Cache , ~/Library/Logs , /Library/Cache and /Library/Logs directories but sometimes, one or more directories are missing and the rm -rf command is not execute. 我有这个小命令,可以删除~/Library/Cache~/Library/Logs/Library/Cache/Library/Logs目录中的所有文件,但是有时,一个或多个目录丢失了,并且rm -rf命令未执行。

sudo find ~/Library/Caches ~/Library/Logs /Library/Caches /Library/Logs -mindepth 1 -type f -exec rm -rf {}+

I wanted the command to ignore missing directories and just execute the command to the files that are found. 我希望该命令忽略缺少的目录,而仅对找到的文件执行命令。

The only issue here is that you are annoyed with seeing the error message about missing directories. 唯一的问题是,您对看到有关缺少目录的错误消息感到恼火。

You may redirect the standard error stream to /dev/null to ignore errors: 您可以将标准错误流重定向到/dev/null以忽略错误:

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

Also note that -mindepth 1 is not needed here, and that some find implementations have -delete : 另请注意,此处不需要-mindepth 1 ,并且某些find实现具有-delete

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

Or, with a shell that understands brace expansions: 或者,使用了解括号扩展的外壳程序:

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

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

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