1834978 Members
2196 Online
110073 Solutions
New Discussion

A script problem

 
ericfjchen
Regular Advisor

A script problem

Hi,

There are some directories under /logs (such as
/logs/bp, /logs/bd, /logs/exec, & etc). The application will generate one log file under these directories every day. It only has to keep the recent 7 days log files. Can you help to provide a script to do it?

Thanks

FJ
5 REPLIES 5
Naveej.K.A
Honored Contributor

Re: A script problem

Hi,

Use logrotate.

You will get logrotate from http://hpux.connect.org.uk/hppd/hpux/Sysadmin/logrotate-2.5/

Regards,
--Naveej
practice makes a man perfect!!!
Suraj Singh_1
Trusted Contributor

Re: A script problem

Hi,

You may write down a crontab entry which runs once every day at some specified time which will remove all files in the specified directory with file modification date greater than 7 days old.

something like:

#minute hour monthday month weekday command
00 17 * * * find /dir_name -depth -mtime 7 -exec rm -f {} \;

Regards
What we cannot speak about we must pass over in silence.
Suraj Singh_1
Trusted Contributor

Re: A script problem

Hi,

You may write down a crontab entry which runs once every day at some specified time which will remove all files in the specified directory with file modification date greater than 7 days old.

something like:

00 17 * * * find /dir_name -depth -mtime 7 -exec rm -f {} \;

Regards
What we cannot speak about we must pass over in silence.
Devender Khatana
Honored Contributor

Re: A script problem

Hi,

You can cron the file containing command which will remove files older than 7 days. Execute it once everyday through cron.

#find /log -mtime +7 -exec rm {} \;

HTH,
Devender
Impossible itself mentions "I m possible"
Muthukumar_5
Honored Contributor

Re: A script problem

You can collect log files which is less than 7 days using find command.

find /logs/ -mtime 7 -name "*" -exec rm -f {} \;

If you find that log files are having same pattern then use it in -name "" option instead of *

Automate this in cron by,

1. put this command in cron tab directly as,

00 0 * * * find /logs/ -mtime 7 -name "*" -exec rm -f {} \; 1>/dev/null @>&1

2. Else put this in a script file and execute it.

#!/bin/ksh
#test.sh
find /logs/ -mtime 7 -name "*" -exec rm -f {} \;

# chmod 755 /tmp/test.sh

00 0 * * * /tmp/test.sh 1>/dev/null 2>&1

hth.
Easy to suggest when don't know about the problem!