Friday, January 28, 2011

find command no such file or directory error

I am using this command to delete the files and folders older than 150 days

 find /media/Server/VPS/dailySQL/* -mtime +140 -exec rm -rf {} \;

The problem is that i get the error like below

find: `/media/May-08-Sat-2010': No such file or directory
find: `/media/May-11-Tue-2010': No such file or directory
find: `/media/May-12-Wed-2010': No such file or directory
find: `/media/May-13-Thu-2010': No such file or directory
find: `/media/May-14-Fri-2010': No such file or directory

why is that

  • Try this:

    $ find /media/Server/VPS/dailySQL/ -mtime +140 | xargs rm -rf
    
    From ErikA
  • Because you're removing the directories, and then trying to descend into them. Add -prune to the end.

  • Problem with a couple of the proposed suggestions is that if any of the files/directories have special characters, they will not be deleted. Doing an -exec in the submitters lins is really time consumming and better efficiency is done by piping the names to xargs and invoking the rm/rmdir in as few times as possible.

     touch /media/Server/VPS/dailySQL/.saver     
     find /media/Server/VPS/dailySQL -type f -mtime +140 -print0 | xargs -0 rm -f >/dev/null 2>&1
     find /media/Server/VPS/dailySQL -depth -type d -print0 | xargs -0 rmdir >/dev/null 2>&1
    

    The second line deletes all the old files. I put a file in the top level directory to save it from destruction on the first line and finally you run through the directories depth-first and simply try to rmdir. If there is something still present, the rmdir will fail. Depth-first is necessary to remove empty sub-directories before a given directory is attempted to be removed.

    Finally, notice -print0 and the -0 parameter on xargs. This allows you to process files that have spaces or other meta characters in them properly. This feature is available on most Linux systems I have been exposed to lately.

    This is the type of scripting I used to remove /tmp and /var/tmp items.

    Enjoy

    From mdpc

0 comments:

Post a Comment