Ubuntu: How to delete folders every N minutes from comand line



Question:

I am am trying to run the below command to delete tmp folders greater than 3 minutes using the below:

 find /tmp/tmp* -mmin +3 -exec rm -rf {} \;  

The above command fails. Why?

find /tmp/tmp* -mmin +3 -exec rm -rf {} \;  find: `/tmp/tmpAJaHLX/crashes': No such file or directory  find: `/tmp/tmpbM5ac8/minidumps': No such file or directory  find: `/tmp/tmpbM5ac8/crashes/events': No such file or directory  find: `/tmp/tmpdQACdf/gmp': No such file or directory  find: `/tmp/tmpo1tbqu/thumbnails': No such file or directory  find: `/tmp/tmpW91Yel/extensions': No such file or directory  


Solution:1

What you're seeing is likely a result of the find command's traversal order.

e.g. given a simple directory

$ find somedir  somedir  somedir/file  

then

$ find somedir -exec rm -rfv {} \;  removed ‘somedir/file’  removed directory: ‘somedir’  find: `somedir': No such file or directory  

You can force a depth-first traversal using the -depth option, i.e.

$ find somedir -depth -exec rm -rfv {} \;  removed ‘somedir/file’  removed directory: ‘somedir’  

does not result in error.

Alternatively, you can use the simpler find somedir -delete which (as noted in the manual page) turns on the -depth option

-delete         Delete files; true if removal succeeded.  If the removal failed,         an  error message is issued.  If -delete fails, find's exit sta‐         tus will be nonzero (when it eventually exits).  Use of  -delete         automatically turns on the -depth option.  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »