LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 02-04-2010, 09:16 PM   #1
agi93
Member
 
Registered: Jan 2010
Posts: 101

Rep: Reputation: 17
Question Clear contents of directory if 45 days old, but exclude specific subdirectory?


I am reorganizing my home directory and have created a few subdirectories which I would like to clean out periodically. Specifically, these are ~/pkg, ~/tmp, and ~/src. I have been looking through the rm man page and googling away to try to figure this out, but I haven't been able to write a script that deletes a file 45 days old (or older) within those directories AND ignores specific subdirectories.

For ~/pkg, I just want to delete all files 45 days old or older.

For ~/tmp, I want to do the same, but leave ~/tmp/preserve and its contents completely untouched.

For ~/src, I want to also delete all files 45 days old or older, but leave ~/src/kernel and its contents completely untouched.

One possible clue to a solution is using the "find" command to locate all files that do not match the name "preserve" and delete those. It was something like
Code:
for f 'find -not -name "preserve"'
do
rm $f
end (or whatever)
I bet that script won't work at all, but the idea is that "find" will locate the desired files and then "rm" will delete them. I had a working example before, but it kept ascending to parent directories and deleting everything!

Does anyone know of a possible solution? I would like to keep a neat little script like this in ~/bin and then run it as a weekly cronjob or something. Thanks for any help!
 
Old 02-05-2010, 01:58 AM   #2
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
find . -path './preserve' -prune -o -mtime +45
Pipe it through xargs or something
 
Old 02-05-2010, 02:39 AM   #3
wailingwailer
LQ Newbie
 
Registered: Oct 2009
Posts: 16

Rep: Reputation: 3
of course, `find` has the -delete flag, so you can probably do everything you're trying to do with a single command. just make sure you test your find flags before you go ahead with any kind of automated destruction.
 
Old 02-05-2010, 01:30 PM   #4
agi93
Member
 
Registered: Jan 2010
Posts: 101

Original Poster
Rep: Reputation: 17
Wow, I didn't know the find command was so versatile! Right now I'm doing some experimenting with that to see if I can get something close to working like I want. Could you please explain xargs? What is the point of it and how can I use it here? The man page (at least on my mac) doesn't seem to explain why I should use it very well.
 
Old 02-05-2010, 02:08 PM   #5
agi93
Member
 
Registered: Jan 2010
Posts: 101

Original Poster
Rep: Reputation: 17
One alternative I found that I might resort to is simply creating a directory "volatile" in my ~/ and having the subdirectories "pkg", "src", and "tmp" within those. Things I want to preserve will be kept in ~/kernel (kernel stuff) and ~/misc (other stuff). I found this find command that deletes all files x days old or older in the specified directory from http://www.howtogeek.com/howto/ubunt...days-on-linux/

It says:
Quote:
Code:
find /path/to/files* -mtime +5 -exec rm {} \;
Note that there are spaces between rm, {}, and \;

Explanation

* The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
* The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
* The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.
So I can just have a script with contents like:

Code:
#!/bin/sh
find /home/agi/volatile/pkg/* -mtime +45 -exec rm {} \;
find /home/agi/volatile/src/* -mtime +45 -exec rm {} \;
find /home/agi/volatile/tmp/* -mtime +45 -exec rm {} \;
And symlink that to /etc/cron.weekly (I'll keep it in ~/bin).

Does this script look safe/usable? I'm not on my actual Slackware box right now and I don't have any files on my Mac that are old enough that I want to clear out. Also, if ~/volatile/[pkg,src,tmp] contains subdirectories, would I substitute "rm" for "rm -r"? And I really need to know if this will ascend to the parent directories or not because I REALLY do not want it to.

Thanks for the help, everyone!
 
Old 02-05-2010, 03:34 PM   #6
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Wow, I didn't know the find command was so versatile! Right now I'm doing some experimenting with that to see if I can get something close to working like I want. Could you please explain xargs? What is the point of it and how can I use it here? The man page (at least on my mac) doesn't seem to explain why I should use it very well.
To save me rewriting all again... http://en.wikipedia.org/wiki/Xargs

You can test your mtime by resetting the date with touch

Code:
touch -d "46 days ago" somefile.txt
Oh and what's with the "*" at the end of your find paths? you know it will return an error if that directory is empty?

Last edited by mRgOBLIN; 02-05-2010 at 03:37 PM.
 
Old 02-05-2010, 04:19 PM   #7
agi93
Member
 
Registered: Jan 2010
Posts: 101

Original Poster
Rep: Reputation: 17
Yes! I got this command to work:

Code:
find /home/agi/volatile/tmp -mtime +45 -exec rm {} \;
after I created a bunch of files with the touch -d command to be 46 days old and created one more brand new file to be preserved. I think a working script would then be:

Code:
#!/bin/sh
find /home/agi/volatile/pkg -mtime +45 -exec rm {} \;
find /home/agi/volatile/src -mtime +45 -exec rm {} \;
find /home/agi/volatile/tmp -mtime +45 -exec rm {} \;
I'll symlink this to /etc/cron.weekly. Hopefully, this doesn't have any bad effects (let me know if you can think of any!)


Thanks, everyone!
 
  


Reply



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
run crontab exclude specific date elainelaw Linux - Newbie 5 12-30-2009 11:53 AM
Regex to exclude a specific phrase grob115 General 5 11-19-2008 12:10 PM
How to exclude specific directories from an rsync backup kaplan71 Linux - Software 2 05-16-2008 12:09 PM
Clear X clipboard contents Wynd Programming 3 01-22-2007 02:01 PM
how can i clear the contents of a file from a C program? tinieprotonjam Programming 5 01-05-2007 11:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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