LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-07-2013, 11:24 PM   #1
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Rep: Reputation: Disabled
remove files older than x days, excluding one of the directory?


hi guys i have done some research and this works for me
to delete files older than 180days
Code:
bash -c 'date;find /data/ -type f  -mtime +180 -exec rm -v {} \;;date' >> /archive/rmlog
lets say there are sub-directory under /data
/data/1
/data/2
...
/data/50

then i have sub-directory
/data/1/a
/data/1/b


i want to run that script to delete all files older than 180days, under directory /data/, but excluding /data/1/a

means the files in /data/1/a will not get deleted after 180days, but files under /data/1/b, and /data/2-50/* will be deleted.

is that a easier way ? else i need to copy paste the script and define the path one by one?
thanks
 
Old 05-08-2013, 12:21 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
To exclude a dir, use the -prune option http://stackoverflow.com/questions/1...-of-find-in-sh.
I'd do this as a separate cmd from the following.

For the others, use a loop that generates the nums 2-50 and feed it to the find cmd.

Put it all in a script instead of calling it on one line, then log output
Code:
./myscript.sh >myscript.sh  2>&1
 
1 members found this post helpful.
Old 05-08-2013, 05:28 AM   #3
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Original Poster
Rep: Reputation: Disabled
hi thanks for the help

lets say, now i have
/data/Apple/1
/data/Ball/1

/data/Cat/Apple/1

when i run
Code:
find /data -name abc -prune -o
/data/Ball/ and others will be deleted.
/data/Apple/ will not be deleted.(which is correct)
but /data/Cat/Apple/ will not be deleted too, which i want them deleted.
i have tried maxdepths, but /data/Cat/Apple is one more sub-directory.

any suggestion? thanks

Last edited by slowerogue; 05-09-2013 at 10:05 PM.
 
  


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
Bash script to remove files older than 3 days rust8y Linux - General 26 10-04-2012 09:26 AM
Bash script to remove files older than x days henkoslaw Linux - General 1 06-10-2012 04:55 AM
[SOLVED] Script: find, compress, remove original files, older than x days? blacklion Linux - General 5 02-27-2012 11:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration