Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scripting help

Status
Not open for further replies.
Oct 9, 2003
174
US
I have a directory that will house oracle dump files and their log files. I am looking for a way that I can keep the latest log\dump files and remove the rest. I have script'ed the section to identify the latest dump file in the directory but i'm having trouble with removing all the files but the one I want to keep.

::EXAMPLE::

the files in the directory are listed below (note the dates will constantly change)

deltekcp_2005042820.dmp.Z
deltekcp_2005042920.dmp.Z
deltekcp_2005043020.dmp.Z
deltekcp_2005050120.dmp.Z
deltekcp_oraexp_2005042820.log
deltekcp_oraexp_2005042920.log
deltekcp_oraexp_2005043020.log
deltekcp_oraexp_2005050120.log

I identified that I want to keep the file *120.log and its corresponding dump file. How can I remove the rest of the files in the directory if their names are alway going to be different while keeping the one that I want.

Is there an easier way than this?:

mv deltekcp_oraexp_2005050120.log deltekcp_oraexp_2005050120.tmp
mv deltekcp_2005050120.dmp.Z deltekcp_2005050120.dmp.Z.tmp
rm deltekcp*
mv deltekcp_2005050120.dmp.Z.tmp deltekcp_2005050120.dmp.Z
mv deltekcp_oraexp_2005050120.tmp deltekcp_oraexp_2005050120.log
 
man find

something like: find !-name "*120.*" -exec rm '{}'

another way is using tocuh to create a file in a given date/time and find files newer than the created one.

man touch
man find

Cheers.
 
another approach I found quite useful:
In a loop, assign all the file names, except for the last, to a variable, and delete them.
Use command substitution and ls command for this, look at -t option of ls;
to omit the last one, consider commands head or tail.

hope this helps
 
This might be something that can help you out:

find /path/to/dir -mtime +2 -exec rm {} \;

-mtime = Modify time, +2 means files that have not been updated in the last two days.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top