Operating System - HP-UX
1821051 Members
2617 Online
109631 Solutions
New Discussion юеВ

Script to delete log files older than 3 days.

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Script to delete log files older than 3 days.

I want a simple script to delete some Oracle's archive log files from different directories which are older that 3 days.

I will put this scrip in crontab which will run everyday night after backups and delete those archive log files older than 3 days.

Thanks,
Everyday Learning.
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Script to delete log files older than 3 days.

Hi:

Here's two ways:

# find / -name *.ARC -mtime +3 -exec rm -f {} \;

The above will remove all files ending in ".ARC" that were modified 3-days ago or more.

Another variation is to create a reference file with a certain date and time. Consider:

# cd /ora_arch
# touch -m -t 10050000 f.$$ #...Oct 5th at 0000 hours..
# find *.ARC -newer f.$$ -exec rm -f {} ;
# rm f.$$

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

Re: Script to delete log files older than 3 days.

Hi (again):

Ah, a few helpful clarifications:

(1) It is best, with 'find' to quote metacharacters. Thus:

# find / -name "*.ARC" -mtime +3 -exec rm -f {} \;

You could also be more specific with the starting directory in the above, e.g. /ora_arch

(2) In the second example I gave you, you would want to NEGATE (!) the 'newer' operator since 'newer ' returns true if the current file has been modified more recently than the argument . ALSO, there is a missing "\". The second example should read:

# cd /ora_arch
# touch -m -t 10050000 f.$$ #...Oct 5th at 0000 hours..
# find . -name "*.ARC" ! -newer f.$$ -exec rm -f {} \;
# rm f.$$

(3) NOTE that the implicit time in the first example is the current time (hhmm). Thus, 3-days in our example is measured as three (3) twenty-four (24) hours increments from the hour and minute the find command is initiated.

...JRF...
Bill Hassell
Honored Contributor
Solution

Re: Script to delete log files older than 3 days.

Just a note about find /

DON'T DO IT.

Many servers are crushed when new users read a Unix For Newbies book and look for files using find /. This command will search through every gigabyte of data on CDROMs, NFS mount points and hundreds of gigabytes of database information. Not good.

When users are looking for some file, have them narrow the search to where the files are really located. Users cannot write files into HP-UX directories like /etc amd /opt so there's no need to overload the server with useless disk activities.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Script to delete log files older than 3 days.

Hi:

Thanks Bill! Yes, heed Bill's advise carefully about "find / ...". If you have a development server, simply time the difference with and without a SupportPlus CD mounted.

...JRF...
Carlos Fernandez Riera
Honored Contributor

Re: Script to delete log files older than 3 days.


Remember that archive log files are needed for database recovery. Better than a time based remove use a backup based remove.

List backuped archive log files on yur backup precedure and then remove it.

unsupported
CHRIS_ANORUO
Honored Contributor

Re: Script to delete log files older than 3 days.

Hi Gulam,

My advise is to list the files to be removed into a file and then remove them. You acn have a look at the file and if need be copy back from tape. You can include this in a script:
find /LIVEORAB ( -name '*.ARC' -o -name '*.log' ) -mtime +3 -print > /LIVEORAB/bkup_graph1
find /oracle ( -name '*.ARC' -o -name '*.log' )-mtime +3 -print > /LIVEORAB/bkup_graph2
Then you can do a remove with this command:
find /oracle /LIVEORAB ( -name '*.ARC' -o -name '*.log' )-mtime +3 -exec rm {} \;



When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
CHRIS_ANORUO
Honored Contributor

Re: Script to delete log files older than 3 days.

Sorry, this excape issue of double slassh.
The lines should read:
find /oracle /LIVEORAB \( -name '*.ARC' -o -name '*.log' \) -mtime +3 -exec rm {} \;



When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Alan Riggs
Honored Contributor

Re: Script to delete log files older than 3 days.

As long as we're dumping on find:

On those rare and annoying occassions when you simply must search every mounted filesystem for a file, please use

du -a / | grep "filename$"

It is much faster and less resource intensive.
Carlos Fernandez Riera
Honored Contributor

Re: Script to delete log files older than 3 days.


Archived redologs resides where log_archive_dest parameter form init$ORACLE_SID.ora points.

You can restrict find to that directory

log_archive_dest = /archived_redo
unsupported