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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a script to removes files by date... 2

Status
Not open for further replies.

Taraporter

Technical User
Apr 1, 2002
26
US
Hi,

I'm looking for a script that will removes files that are in a directory and are older then 30 days. Basically, I need a script that will help clean up a log directory.

All the scripts that I have seen so far are 30 lines long and look to be too complex. I was looking for something simple.

Thanks.
Tara
 
A good script will take 1 line to do the job and 30 lines to make sure its done RIGHT.

***************************************
Party on, dudes!
[cannon]
 
Code:
find logs -type f -mtime +30 -print | xargs rm
Note, this will recurse starting from the logs directory. If you want only to prune files in logs, and ignore files in subdirectories, then use:
Code:
find logs \( -type d ! -name logs -prune \) -o \( -mtime +30 -print \) | xargs rm
Test these thoroughly first, by replacing xargs rm with xargs
Cheers, Neil
 
The simplest solution is to use find. Here is a sample find command that will delete everything older than 30 days:
Code:
find <APP_DIR>/log -mtime +30 -exec rm {} \;

The problem lies in making sure you don't delete anything other than the specified log files. If there is some regular expression (RE) to identify the log files, you can add that to the find command to limit the search:
Code:
find <APP_DIR>/log -name &quot;*.log&quot; -mtime +30 -exec rm {} \;

Simply change the &quot;*.log&quot; to what will fit your needs.

Also, remember that the find command will start at the directory given and keep working down recursively. So if you have other files in a directory below the directory you start at, they may get deleted too.

It would be good to do a test first replacing the &quot;-exec rm {} \;&quot; part with &quot;-print&quot; so that you will know exactly which files will be deleted.

Good luck, and remember that UNIX doesn't have an easy undelete. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Thanks for the responses. This was exactly what I was looking for. Since the directory was created specifically for log outputs, there is no danger of deleting anything other then a log file. Thanks again.

Tara
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top