LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-25-2015, 03:08 PM   #1
linux_nerd
LQ Newbie
 
Registered: Jun 2015
Posts: 26

Rep: Reputation: Disabled
Exclude directories with find to clean up a filesystem running redhat6.6.


Hello all, I want to keep my system clean and in order so I have decided to write a script that will remove files that have not been accessed in 20 days. What I need help doing is excluding certain directories. Here is code I plan on using to accomplish this, I have not found anything that points me to how to exclude a directory.
Code:
find /tmp -type f -mtime +20 -delete
 
Old 08-26-2015, 08:00 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Oh, this one's gonna be fun.

Code:
find /tmp -type d -iname "<directory>" -prune -o -type f -mtime +20 -delete
Repeat -type d -iname "<directory>" -prune -o for each desired directory to keep. I'd replace -delete the first few times with -print to make sure it behaves as you want.

Oh you can use grep,

Code:
find /tmp -type f -mtime +20 -delete | grep -v -E '<directory_to_keep>|<dir2>|..|<dirN>' | xargs rm -f
Must be single quotes, or the shell's going to do it's expansion thing and the results will not be what you want.
 
Old 08-26-2015, 08:14 AM   #3
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
I figured a one liner would suffice.

Alas, now you've got me thinking, damn you, that I could write a pretty neat script around this. I don't have the time now, but if no one else posts one, maybe I will later.
 
Old 08-26-2015, 09:05 AM   #4
linux_nerd
LQ Newbie
 
Registered: Jun 2015
Posts: 26

Original Poster
Rep: Reputation: Disabled
Sorry to get you thinking LOL, but thanks for the pointers
 
Old 08-26-2015, 04:33 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by goumba View Post
I figured a one liner would suffice.

Alas, now you've got me thinking, damn you, that I could write a pretty neat script around this. I don't have the time now, but if no one else posts one, maybe I will later.
tmpwatch?
 
Old 08-26-2015, 07:48 PM   #6
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by Habitual View Post
tmpwatch?
Now you ruined my fun I actually didn't know that existed and was going to write a script, but no need to now.
 
  


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
Using Find with an exclude/exclude file metallica1973 Linux - General 8 11-06-2011 09:39 PM
[SOLVED] How do I exclude multiple directories in awk with find? Damarr Linux - Newbie 11 05-24-2010 02:52 PM
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 12:36 AM
exclude directories from find noir911 Linux - General 3 11-22-2006 06:39 AM
Running find on / results in filesystem error Maeltor Linux - General 5 06-07-2006 11:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:23 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