LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Exclude files from rm -rf based on date (https://www.linuxquestions.org/questions/programming-9/exclude-files-from-rm-rf-based-on-date-699493/)

stefaandk 01-23-2009 07:08 PM

Exclude files from rm -rf based on date
 
I am using this command to remove files from a directory based on date

Quote:

/usr/bin/find /var/backup/sitebackup/ -mtime +5 -exec rm -rf "{}" ";"
But I have one folder in here .notdelete

Which also gets deleted with this command, I can work around this by backing up to different paths etc but I would nevertheless like to know if it's possible to adjust the above command to still delete all content except the .notdelete directory.

Thanks

ta0kira 01-23-2009 09:46 PM

The easiest way would be to pipe the results of find through grep, then use it as an argument to rm. You could use name patterns with find, but you might not be able to prevent the deletion of the contents of the directory easily.
Code:

rm -rfv $( /usr/bin/find /var/backup/sitebackup/ -mtime +5 -depth | egrep -v '(^|/)\.notdelete($|/)' )
ta0kira

edit:
I should have looked at the manpage:
Code:

/usr/bin/find /var/backup/sitebackup/ '!' -wholename '*/.notdelete/*' -a '!' -name .notdelete -mtime +5 -exec rm -rf "{}" ";"

ErV 01-23-2009 10:20 PM

Quote:

Originally Posted by ta0kira (Post 3419278)
The easiest way would be to pipe the results of find through grep,

I think find supports logical operations:
Code:

OPERATORS
      Listed in order of decreasing precedence:

      ( expr )
              Force precedence.

      ! expr True if expr is false.

      -not expr
              Same as ! expr, but not POSIX compliant.

      expr1 expr2
              Two expressions in a row are taken to be joined with an implied "and"; expr2  is  not  evaluated  if
              expr1 is false.

      expr1 -a expr2
              Same as expr1 expr2.

      expr1 -and expr2
              Same as expr1 expr2, but not POSIX compliant.

      expr1 -o expr2
              Or; expr2 is not evaluated if expr1 is true.

      expr1 -or expr2
              Same as expr1 -o expr2, but not POSIX compliant.

      expr1 , expr2
              List;  both expr1 and expr2 are always evaluated.  The value of expr1 is discarded; the value of the
              list is the value of expr2.      The comma operator can be useful for searching for several  differ-
              ent  types of thing, but traversing the filesystem hierarchy only once.  The -fprintf action can be
              used to list the various matched items into several different output files.

See "man find" for details.

So using something like
Code:

-mtime +5 -and -not -iname \*.notdelete\*
should work. Haven't tested it, though.

syg00 01-23-2009 10:24 PM

Isn't this what "-prune" is for ?.


All times are GMT -5. The time now is 09:30 PM.