System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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
Matti_Kurkela
Honored Contributor

Re: Linux script for purging old files

The -ctime, -mtime and -atime options of the find command calculate the file's age by 24-hour periods, ignoring any fractional part.

"-mtime 7" finds files which are _exactly_ 7 days old, i.e. files with ages between [exactly 7 days] and [7 days, 23 hours, 59 minutes and 59 seconds].

"-mtime +7" finds files which are more than 7 full 24-hour periods old, i.e. 8 days old or older. A file that is 7 days, 23 hours, 59 minutes and 59 seconds old is counted as 7 days old, and "more than 7" means 8 days or more.

MK
MK
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

Much better explainations...Thank you!
Jorge Cocomess
Super Advisor

Re: Linux script for purging old files

Thanks!