Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 829
yaeykay Avatar asked Dec 01 '25 06:12

yaeykay


1 Answers

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:

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:

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
like image 54
Kusalananda Avatar answered Dec 03 '25 23:12

Kusalananda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!