I have a script that deletes files older than N minutes. The script basically runs:
find /some-folder/* -mmin +59 | xargs rm -rf
But for some reason it prints tons of:
No such file or directory but seems to do the work, e.g. see below example:
$ du -hs /opt/storage/ 5.2G /opt/storage/ $ find /opt/storage/* -mmin +59 | xargs rm -rf find: /opt/storage/caches/modules-2/files-2.1/org.jboss/jboss-parent: No such file or directory find: /opt/storage/caches/modules-2/files-2.1/redis.clients: No such file or directory find: /opt/storage/caches/modules-2/files-2.1/org.infinispan: No such file or directory ... $ du -hs /opt/storage/ 4.0K /opt/storage/
Any ideas to what is causing
find combined with
xargs to find files that does not exists? Maybe some race condition where files are somehow deleted before they are found/printed to log?
but mentions that the solution is to use
xargs which is what I am doing.
Update: Based on below comments seems the issue is that deleting a file updates the modified time for the parent folder. Also files and folder will potentially be created during the deletion under the same parent/starting folder.
There is an example in this post: https://stackoverflow.com/a/13869000/363603
ctime instead. But from:
Changed time The last time the file’s metadata, attribute or content was changed.
so that would give the same issue.
If its possible to combine
that would probably be the most robust solution but so far I have not found examples for this.
So there is no "standard" way in linux to do file retention (based on a date threshold) recursively from a starting folder while some parts of the subtree might be updated during the deletion/retention?
Current solution: Based on findings so far I have split into finding all the files for deleting followed by executing the deletion:
# Store path to all files that should be deleted (potentially a lot!) FILES_TO_DELETE=$(find /some-folder/* -mmin +59) # Delete the file rm -rf $FILES_TO_DELETE