Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to remove files older than....

Status
Not open for further replies.

Technobie

MIS
Oct 13, 2003
18
US
Hello all,

I need a little help creating a script that would go out to a certain directory(s) and remove files that are older than 14 days. Obviously would run it as a cronjob, but I'm a little rusty on the actual scripting.

Would it go something like this?

find /export/home/* -mtime +14 -maxdepth 0 -exec rm -rf {} \;

Any comments or suggestions would be greatly appreciated!

 
Yikes! No!

That "[tt]-r[/tt]" you have after the [tt]rm[/tt] will cause it to recursively delete a whole directory tree. As soon as it hits a directory that's older than 14 days, that whole directory tree and all of it's contents are gone, even if the contents are "younger" than 14 days.

If you wanted to limit it to files and leave directories alone, you could add a "[tt]-type f[/tt]" right after the directory spec.

Hope this helps.

 
you have to add a `-t f' option
(to delete just regullar files)
let's suppose you have:
a dir
D1 - 20 day older
D1_1 - a subdir of D1 , just modified
when your `find' will find D1, will delete the entire subtree directory.
So, you have to add 't f' to find.
Also is a good idea to cut `-r' option from `rm' command.
(you will keep some dirs, but the space is not so big for an empty dir)

PM
 
sorry:
replace `-t f', with `-type f'

The same solution like sambones - few seconds later :)
 
I just realized what a silly mistake I made with that -r. Thank you very much SamBones for pointing that out. I'll give it a shot!

Chris Driver
//UNIX like TeePee. No Windows, No Gates, Apache inside.\\ :D
 
Here is what ended up working in my directory.

example: find /export/home/cdriver/* -type f -mtime +14 -exec rm -f {} \;

Thanks for all your help!

Chris Driver
//UNIX like TeePee. No Windows, No Gates, Apache inside.\\ :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top