cohortq
cohortq

Reputation: 687

Bash Script OSX not deleting files it finds

I have this bash script here

#!/bin/bash
find /Users/ -name "*.mov" -o -name "*.flv" -o -name "*.mp4" -o -name "*.avi" -o -name "*.wmv" -o -name "*.mpeg" -o -name "*.avi" -o -name "*.wmv" -o -name "*.f4v" -o -name "*.m4v" -o -name "*.mxf" -o -name "*.ts" -type f -mtime +7 -exec rm -rf {} \;

It finds all the files that are older than 7 days, and that works fine, but when I want it to remove the result set that I found it doesn't delete any of the files. Is there something I'm doing wrong? This is on Mac OSX 10.6

Any help would be great. Thanks!

Upvotes: 0

Views: 1113

Answers (3)

Gordon Davisson
Gordon Davisson

Reputation: 125708

The find command is in /usr/bin, which isn't in the default PATH for cron jobs. Either run it as /usr/bin/find, or set PATH at the beginning of your script.

Upvotes: 0

David W.
David W.

Reputation: 107040

Instead, of -exec rm -rf {}\;, try the -delete option if it's available on your version of the find command. This will show an error message after each failed attempt to delete. That might give you more information what's going on.

$ find . -name "*.foo" -type f -mtime +7 -delete
find: -delete: unlink(./four.foo): Permission denied
find: -delete: unlink(./one.foo): Permission denied
find: -delete: unlink(./three.foo): Permission denied
find: -delete: unlink(./two.foo): Permission denied

Neither find is returning the actual exit code from the delete/rm command. You may want to do something like this:

find . -name ... -type f -mtime +7 | while read file
do
   if rm -fr $file
   then
      echo "Successfully deleted $file"
   else
      echo "Error deleting file: Exit code $?"
   fi
done

That might give you a better understanding of what's going on.

Upvotes: 1

itsafire
itsafire

Reputation: 6083

Maybe you should run the command with sudo ? You may not have full access to all directories as a normal user.

Upvotes: 0

Related Questions