LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2009, 07:08 PM   #1
stefaandk
Member
 
Registered: Jun 2005
Distribution: Centos
Posts: 215

Rep: Reputation: 30
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
 
Old 01-23-2009, 09:46 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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 "{}" ";"

Last edited by ta0kira; 01-23-2009 at 09:52 PM.
 
Old 01-23-2009, 10:20 PM   #3
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by ta0kira View Post
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.
 
Old 01-23-2009, 10:24 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Isn't this what "-prune" is for ?.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 01:21 AM
Delete files based on current date -1 Winanjaya Linux - Newbie 3 06-23-2008 04:38 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 07:54 PM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 04:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration