Operating System - HP-UX
1833852 Members
2018 Online
110063 Solutions
New Discussion

Re: Automatic File Removal script

 
SOLVED
Go to solution
Joseph Bague
Frequent Advisor

Automatic File Removal script

I have a daily routine to delete a five day's old file (Oracle archive log). Can you give a me sample script that will delete a five day's old file.

Many thank's
Expect nothing but ready for everything
9 REPLIES 9
H.Merijn Brand (procura
Honored Contributor

Re: Automatic File Removal script

using find


find . \! -mtime 5 -exec rm -f {} \;

test first!
Enjoy, Have FUN! H.Merijn
Stefan Farrelly
Honored Contributor

Re: Automatic File Removal script

We always use;
cd
find . -mtime + -exec rm {} \;

The + is usually +7 or +14 for one or two weeks or +30 for a month. Im not sure what the earlier reply using find . \! -mtime does!?
Im from Palmerston North, New Zealand, but somehow ended up in London...
H.Merijn Brand (procura
Honored Contributor

Re: Automatic File Removal script

\! -mtime 7
=
-mtime +7

but older (read very old) find versions do not support + notation

exclamation mark is 'not' or 'reverse' and is escaped for (t)csh users

HTH

BTW GNU find has many more nifty features
Enjoy, Have FUN! H.Merijn
V. V. Ravi Kumar_1
Respected Contributor

Re: Automatic File Removal script

hi,
find -type f -mtime +5 -exec rm {} \;

here if u say 5 it deletes files which are modified exactly 5 days back, if us say +5 it deletes files modified 5 or more days back, and -5 deletes files which are modified during last 5 days. here mtime modification time, u can also specify atime (accessed time) there.
type f indicates it is a file.

regds
Never Say No
James Beamish-White
Trusted Contributor

Re: Automatic File Removal script

You need to be very careful with these, to make sure that your don't start using variables which may be empty, and which may effectively rm -R / (trust me - I've done it ;-)

I would use something like:


find /your/log/path -name "*log" -mtime 5 | xargs rm

The xargs is a bit better on performance than find/exec. The use of a full path means you won't remove anything from another path, and the -name part again limits what you will be deleteing.

Cheers!
James
GARDENOFEDEN> create light
George Petrides_1
Honored Contributor
Solution

Re: Automatic File Removal script

Just because I saw people before making mistakes in the find path... you might want to specify a name pattern for the archive logs... e.g.
find /ORACLE_ARCH_DIR -mtime +5 -name "ora_arch*" -type f -exec rm -f {} \;
In UNIX you have to make sure you protect yourself from making mistakes else the results can be catastrophic...
George
Jean-Luc Oudart
Honored Contributor

Re: Automatic File Removal script

Here's is a scripts we run on a monthly basis.
1st it prints the list of file we're about to delete, then run the deletion.
We delete files with different patterns.
See attachment

Jean-Luc
fiat lux
Vytautas Vysniauskas
Occasional Contributor

Re: Automatic File Removal script

Hi,

Some of proposed solutions are simple but realy dangerous. Removal of old files sometimes can be very tricky. There are 2 MUST rules for any script or program performing cleanup of filesystem

1. Do not follow symlinks in the directories (even if it is the top level directory for cleaning).

2. Remove only regular files and EMPTY directories.


Personally I prefer to use a 'tmpwatch' program which comes with many versions of RedHat Linux. Source code is available on Redhat web site.
The source is tested to compile and work correctly on
HP-UX as well.


Vytas.
Vytautas Vysniauskas
Joseph Bague
Frequent Advisor

Re: Automatic File Removal script

Thank's to every one :)
Expect nothing but ready for everything