I am running a find and rm -rf directory list modified earlier than 7 days with an exclusion list of directories which should not be deleted at any case.

This is the script excerpt

find [path-to-base-dir] -maxdepth 1 -type d ! -name [dir1] ! -name .snapshot \
! -name . ! -name [dir2] ! -name [dir3] ! -name [dir4] ! -name [dir5] ! -name [dir6] \
! -name [dir7] ! -name [dir8] ! -name [dir9] ! -name [dir10] ! -name [dir11] ! -name [dir12]\
 -mtime +7 -exec rm -rf {} \; -print

This is scheduled as a daily cron. On most days, this works fine.. But on some days it ignores the exclusion list and deletes all directories. How can this happen? The condition list is very long, so any kind of latency can lead to find command ignoring the !name conditions..?

  • Small tip: Rather than -print you can use the -v option to rm to simplify the command. – l0b0 Feb 10 '15 at 9:20
  • Do you give the base path as a relative or absolute path? How do you give the exclusions? Relative or absolute? – 0xC0000022L Feb 10 '15 at 9:23
  • 2
    I worry re this part ! -name . What that mean in your script? If you wants to exclude base dir you should better use -mindepth 1 parameter. – Costas Feb 10 '15 at 9:30
  • @Costas, or use find "$basedir/." or cd -P -- "$basedir" && find ., not all find implementations have -mindepth. – Stéphane Chazelas Feb 10 '15 at 10:03
  • Is the command static or generated? Are there any special characters (including whitespace, dashes, exclamation marks etc) in any of the directory names? – l0b0 Feb 10 '15 at 12:30
find path-to-base-dir -maxdepth 1 \
     -type d ! -name bunch-of-exceptions \
     -mtime +7 -exec rm -rf {} \; -print

You did not include path-to-base-dir in the bunch-of-exceptions. (You included . but that would only match if path-to-base-dir was exactly .) The only condition that the directory path-to-base-dir might fail if -mtime +7. If the directory happens not to have been modified in the last 7 days, it will be deleted.

Since your find has -mindepth and -maxdepth, use -mindepth 1 to exclude the starting directory.

  • Thank you. Yes the issue was with '! -name .' It was not including the parent directory, unless the command is run from the same location, and that too only if 'find .' is used; if you use absolute path for find and use '! -name .' for matching, that will also fail. Our intermittent delete issue was due to the fact that most of the days the parent directory was excluded due to the last 7-days modified condition, and not because we had '.' in exclusion. the parent directory gets modified if any file or directory is added/deleted directly under it. thanks again. thanks to costas and other too. – Vinod V Feb 13 '15 at 7:38

Put those [dir1] strings in quotes, otherwise the shell will try to expand them because of the brackets, which may or may not succeed according to the contents of your current working directory.

  • 1
    I doubt OP meant literally [dir1]. – l0b0 Feb 10 '15 at 9:19
  • Sorry, the directory names are different, e.g., suggestapp, content, etc. I just represented them here like [dirx]. '.' is used to exclude current directory and .snapshot to exclude some nfs backups. We dont use quotes for the file names, but as I said this works fine most of the days. Fails in a few days with no pattern, maybe once in a month or 2 times a week, and so on. – Vinod V Feb 10 '15 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.