2

I'm using Amazon Linux. I have this statement that is designed to remove files in a directory

/bin/find $JBOSS_HOME/standalone/tmp/vfs -mindepth 1 -mmin +1441 -exec rm -rf {} \;

But this results in a bunch of statements like this

/bin/find: `/usr/java/jboss/standalone/tmp/vfs/deployment2b6836209f40596a': No such file or directory
/bin/find: `/usr/java/jboss/standalone/tmp/vfs/deployment1fae3ca49edb3384': No such file or directory
/bin/find: `/usr/java/jboss/standalone/tmp/vfs/deploymenta2b0c99bfdd7f444': No such file or directory
...

Is there a way to have my statement not scan files that don't exist, or at least eliminate all these errors?

3
  • 6
    Add a -prune after the -exec ... \; so find doesn't attempt to descend into the directories you've just removed. Mar 10, 2017 at 15:29
  • 2
    Or do a depth-first search so deeper stuff is dealt with first.
    – thrig
    Mar 10, 2017 at 15:41
  • As a note, to get rid of any error statements from the output at the end of almost any statement, pipe stderr to /dev/null. 2> /dev/null Mar 10, 2017 at 16:22

1 Answer 1

4

Beware that your command removes directories that haven't changed in a day, even if some files under them have changed recently.

Assuming that this is what you want, Stéphane Chazelas and thrig have proposed two solutions in comments. The problem you're facing is that you instruct find to delete a directory, and then it tries to traverse it to see if there are any files in it that would match.

  • Pass -depth to tell find to process a directory's contents first and the directory itself last. This wastes a bit of time analyzing the contents of directories that will be removed just later.

    find $JBOSS_HOME/standalone/tmp/vfs -depth -mindepth 1 -mmin +1441 -exec rm -rf {} \;
    
  • Use -prune to instruct find not to explore the directories that are getting removed.

    find $JBOSS_HOME/standalone/tmp/vfs -mindepth 1 -mmin +1441 -exec rm -rf {} \; -prune
    

If you only meant to delete old files, then let find know. You can use -delete instead of invoking rm.

find $JBOSS_HOME/standalone/tmp/vfs -mmin +1441 -type f -delete

You can then remove empty directories:

find -depth $JBOSS_HOME/standalone/tmp/vfs -type d -empty -delete

Or combine the two, deleting directories after any file in them that should be removed has been removed:

find $JBOSS_HOME/standalone/tmp/vfs -depth -mindepth 1 -mmin +1441 -type f -delete -o -type d -empty -delete

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .