Operating System - HP-UX
1833064 Members
2869 Online
110049 Solutions
New Discussion

How to delete the logs which are older than 7 days

 
SOLVED
Go to solution
praveen..
Super Advisor

How to delete the logs which are older than 7 days

Hi,
I need a script which can remove the log files which are older than 7 days.

All the logs file are in /tmp/logs directory.

Please suggest

will this script be same for RHEL?

Thanks
Vipin
6 REPLIES 6
Chan 007
Honored Contributor
Solution

Re: How to delete the logs which are older than 7 days

HI,

Use the to find files older than 1 year

find /tmp -mtime +365 -exec ls -l {} \;

Deleting

find /tmp -mtime +365 -exec rm -rf {} \;

Chan
Yogeeraj_1
Honored Contributor

Re: How to delete the logs which are older than 7 days

hi vipin,

try the following:
find /tmp/logs/ -xdev -name "*" -mtime +7 -exec rm {} \;


to be cautious, please verify that the files you are deleting are ok by running:

find /tmp/logs/ -xdev -name "*" -mtime +7 -exec file {} \;

or
find /tmp/logs/ -xdev -name "*" -mtime +7 -exec ll {} \;


should work fine with RHEL as well.

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Peter Godron
Honored Contributor

Re: How to delete the logs which are older than 7 days

Hi,
you could try:

find /tmp/logs \( -type d ! -name . -prune \) -o \( -type f -mtime +7 ! -name "." -print \) -exec rm {} \;

Berd
Trusted Contributor

Re: How to delete the logs which are older than 7 days

Vipin,

I would use

find /tmp/logs -type f -mtime +7 -exec rm {} \;

Berd
James R. Ferguson
Acclaimed Contributor

Re: How to delete the logs which are older than 7 days

Hi:

This question is so commonly asked that the manpages for 'find' even have an example!

http://www.docs.hp.com/en/B2355-60127/find.1.html

Regards!

...JRF...
praveen..
Super Advisor

Re: How to delete the logs which are older than 7 days

Thanks