Operating System - Linux
1752520 Members
4714 Online
108788 Solutions
New Discussion юеВ

Re: Linux script for purging old files

 
SOLVED
Go to solution
Jorge Cocomess
Super Advisor

Linux script for purging old files

Greetings,

I am looking for help on writing a script to purge files that are older than 14 days. I am currently running RHAS 4.0

Please help.

Thanks,
Jorge
12 REPLIES 12
Dennis Handly
Acclaimed Contributor

Re: Linux script for purging old files

You can use find:
find some-path... -mtime +14 -exec rm -rf \;

Make sure you test it first by inserting the echo command before "rm".
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

How would I or how I should retain a list of purged files?

Thanks!
Ivan Ferreira
Honored Contributor

Re: Linux script for purging old files

Use rm -fvr, adding the "-v" will display the name. Redirect the ouput to a file with "> filename".
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

Okay...

So, the codes should look like this?

cd /tmp/log_files
find . -type -f -mtime 2 -name "*.log*" -exec rm -fvr > /tmp/purged.log {} \;

Thanks much!!

Jorge
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

I need help with my script. I am very novice and still learning on how to write Linux scripts at this time. There are two (2) issues with what I am trying to do.

1. The script execute but does nothing. No errors.

2. I have results if I execute the command from the script manually. However, it showed that the log captured the removed files, but leaves the supposely removed files back in the original directory.


Here's the sample of my script.

#! /bin/sh
#
# Delete files that are older than 7 days
cd /tmp/datalog/tdump
find . -type f -mtime 7 -name "*.dlog*" -exec rm -fvr > \tmp\purge.log {} \;



feedback would greatly appreciate it.

Jorge
Court Campbell
Honored Contributor
Solution

Re: Linux script for purging old files

#! /bin/sh
#
# Delete files that are older than 7 days
cd /tmp/datalog/tdump
find . -type f -mtime 7 -name "*.dlog*" -exec rm -fvr {} \; > \tmp\purge.log
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

I think I also got the back-slash backward as well.
find . -type f -mtime 7 -name "*.dlog*" -exec rm -fvr > \tmp\purge.log {} \;

It should be
find . -type f -mtime 7 -name "*.dlog*" -exec rm -fvr {} \; > /tmp/purge.log

I will test it out and let you all know.

Thanks!!
Dennis Handly
Acclaimed Contributor

Re: Linux script for purging old files

>Delete files that are older than 7 days

Older requires: -mtime +7
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

"Older requires: -mtime +7" - It doesn't work for me. It will work just fine with -mtime 7. Beats the heck out of me.

Any ideas??

J