Operating System - HP-UX
1819504 Members
3044 Online
109603 Solutions
New Discussion юеВ

removing a log file that is 5 days old or older

 

removing a log file that is 5 days old or older

Hi
I know you have been asked about this before.

HP-UX 11.11 and 11.23

We have old log files that need to be deleted that are older the 4 day old and would like to know the most automated way of doing this.

We will have this run as a crontab too so that part I do not need to know. We know in Linux you can do this but we are using HP-UX.

Steve
9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor

Re: removing a log file that is 5 days old or older

Well, you don't give many details (such as pathnames) but here's one the will look in /var/tmp and /xxx/yyy for regular files that end with .log older than 4 days.

find /var/tmp /xxx/yyy -type f -name '*.log' -mtime +4 -exec rm {} \;

I would replace the -exec rm {} \; with a more innocuous command (e.g. -exec ls -l {} \; ) until I got the find filters just right. You could also many the command more efficient by piping to xargs to group the rm's but that's overkill for a small number of files. Man find for details (and man xargs if you like).
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: removing a log file that is 5 days old or older

Hi Steve:

It's as simple as:

# find /path -xdev -name "*.log" -mtime +4 | xargs rm

...This finds any files named "*.log" that are older than 4-days (i.e not modified in that period).

Using '-xdev' prevents 'find' from crossing mountpoints. The double quotes around the name prevent the shell from expanding the metacharacters before the token is passed to 'find'. The use of 'xargs' avoids '-exec' which spawns a separate task for each file to be removed. Thus, 'xargs' makes your process kinder on system resources.

You can also add a '-t' switch to 'xargs' to create a trace of what will be removed. See the 'xargs' and 'find' manpages for more information.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: removing a log file that is 5 days old or older

Hi (again) Steve:

Ooops! I forgot to limit the removal to *files* ( '-f' ). The 'find' should be:

# find /path -xdev -type f -name "*.log" -mtime +4 | xargs rm

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: removing a log file that is 5 days old or older

Shalom Stephen,

find /var/adm/syslog -type f -name "nameoflog" -mtime +4 | xargs rm

You can use wildcards.

You could also do a find and and -exec the results to rm -f

The key thing here is +mtime

A google search should have turned it up.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=998415
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1024386
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=122004

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Rick Garland
Honored Contributor

Re: removing a log file that is 5 days old or older

Look into a utility like "logrotate" that will manage all of your log files. You can specify daily, weekly, monthly, file size, specific dates, etc. Will delete them, restart the processes, gzip them, send you mail if you want, etc. Can have different actions for different log files (delete logA weekly and delete logB monthly)

Lots of possibilities to manage the log files!

Available on the http:/gatekeep.cs.utah.edu site


f. halili
Trusted Contributor

Re: removing a log file that is 5 days old or older

# find /sourcedir -mtime +4 -type f -local -exec rm {} \; > /tmp/logfile.txt
derekh
f. halili
Trusted Contributor

Re: removing a log file that is 5 days old or older

This is more complete. The one I sent earlier assumes that only logs are in /sourcedir

# find /sourcedir -name '*.log' -mtime +4 -type f -local -exec rm {} \; > /tmp/logfile.txt
derekh

Re: removing a log file that is 5 days old or older

From all your help I have what I need ...

find $LOGDIR/link* -type f -mtime +1 -exec mv {} $LOGDIR/Link-Archive \;
find $LOGDIR/*log -type f -mtime +1 -exec gzip {} \;
find $LOGDIR/*gz -type f -mtime +5 -exec rm {} \;

Thanks to all of you we have a solution -- thanks

Steve

Re: removing a log file that is 5 days old or older

thanks for all the help -- :)