cancel
Showing results for 
Search instead for 
Did you mean: 

Shell Script help

Suman_7
Frequent Advisor

Shell Script help

All,

I need to write a shell script that connects to oracle and queries a datadictionary to get a filename. Now the script has to delete all the files on the unix box that were created before the creation_timestamp of the filename returned.

Can someone please send me a sample code?

Thank You
7 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Shell Script help

Hi:

First, there is no such thing in Unix as a "creation" timestamp. You have a "last modification" time that represents a creation moment when I file or directory is first instantiated. Thereafter, any change in the file or directory contents updates the modification timestemp.

That said:

# find /path -xdev -type f ! -newer reffile -exec rm {} \+

This will find files ('-type f') in '/path' while not crossing mountpoints ('-xdev') that are not more recently modified ('! -newer') than a 'reffile' that you can create or already exists. File meeting these criteria will be removed.

Should you need to create a "reference file", simply do:

# touch -amt yymmddhhmm reffile

...for example for February 29 at 1300 hours:

# touch -amt 0802291300 reffile

See the manpages for 'find' and 'touch' for more information.

Regards!

...JRF...
Tim Nelson
Honored Contributor

Re: Shell Script help

Below is a great place for stuff like this.

http://forums.oracle.com/forums/categoryHome.jspa?categoryID=84

Suman_7
Frequent Advisor

Re: Shell Script help

Thank You very much!

The find command you gave me also include the filename in the result set. Is it possible to exclude this file from the result as I need the files modified before "this" file. and I need only files with *.arc to be removed.

Steven Schweda
Honored Contributor

Re: Shell Script help

> [...] Is it possible to exclude this file
> [...]

Put it somewhere where "find" won't find it?

touch -amt yymmddhhmm /tmp/reffile
[...] -newer /tmp/reffile

If you use "/tmp" (or some other common
location), you might wish to throw a process
ID into the file name, in case you try this
twice at the same time.
James R. Ferguson
Acclaimed Contributor

Re: Shell Script help

Hi:

> The find command you gave me also include the filename in the result set. Is it possible to exclude this file from the result as I need the files modified before "this" file. and I need only files with *.arc to be removed.

Well, you didn't specify those elements, but that's easily done:

...for example for February 29 at 1300 hours:

# touch -amt 0802291300 /var/tmp/reffile

# find /path -xdev -type f -name "*.arc" ! -newer /var/tmp/reffile -exec rm {} \+

Note that the '-name' argument is quoted so that the shell doesn't expand the wildcard characters before 'find' has an opportunity to evaluate it.

Regards!

...JRF...
Yogeeraj_1
Honored Contributor

Re: Shell Script help

hi,

Which version of Oracle are you running?

All this pain of writing a script to do this unhealthy maintenance can be avoided by using RMAN to backup your archived log files and automatic purging.

revert.

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Suman_7
Frequent Advisor

Re: Shell Script help

We are running 10g . But this is needed for Oracle Streams. Streams Capture process needs certain old archivelog files to restart when instance is bounced.