I have a purge job that runs daily to clean up logs older than 30 days.

find /dir/app/logs -mtime +30 -exec rm -f {} \;

I am moving our jobs out of cron and into a 3rd party scheduling product, Automic. Since moving this job, I keep getting the error "No such file or directory" randomly. Running the find command at the prompt after receiving the error, without the -exec rm -f {} \;, always returns no results and runs successfully. Long story short, I'm unable to reproduce the error.

The job runs by executing the command:

ssh user@server "find /dir/app/logs -mtime +30 -exec rm -f {} \;"

against the remote server.

I have tested various solutions without any luck. Originally, the command ran without -f. Adding -f, I understand, is supposed to suppress errors, but I'm not seeing that happen. I tried replacing -exec rm {} \; with -delete, but that didn't help either.

Currently I'm testing changing \; to + as suggested here:

find - exec rm vs -delete

Thanks in advance for any insight into what's going on.


After some time off and more googling around, this solution may fix the issue:

find /usr/dir/logs/ -mindepth 1 -mtime +45 -delete

It appears find and rm were including the parent directory and trying to remove it first, which explains why it sometimes worked, then would throw missing file/directory errors. Using -delete worked fine, and adding -mindepth 1 caused it to skip the parent.

To retain subdirectories where they shouldn't be deleted, adding -type f to only look at files left those be. Additionally, there was one instance where a specific file needed ignored. This appears to work for that situation:

find /usr/dir/logs/ -mindepth 1 -type f \( ! -name "ignoreme*" \) -mtime +30 -delete
|improve this answer|||||
  • 2
    Also, with -delete, it will do a depth-first traversal of the directory structure. This means that if you do delete directories (obviously you don't with -type f), then any directory that you delete will already have been processed previously (find won't try to access that directory again). You may force a depth-first traversal without -delete using -depth. – Kusalananda Jan 11 '19 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.