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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

erase files after a certain date

Status
Not open for further replies.

SM777

Technical User
Mar 7, 2001
208
GB
Is it possible to erase files after a certain date using rm?

e.g.

log061202.log 12-Jun-2002
log061102.log 11-Jun-2002

I want to get rid of all files which are older than 7 days. In this case log061102.log. I can either delete by filename or by timestamp.

I can't see how this can be done using rm as the manual doesnt give any clues. I guess I have to use a perl script?
 
You could use find as follows:

find <directory> -name '*.12-Jun-2002' -exec rm {} \;

or

find <directory> -mtime +7 -exec rm {} \;

As always, when using find and rm in this way, make sure you know where you are and that the results will be what you require by using find without the rm first (use ls instead for example).

Hope this helps.
 
If this works for you, then you can create a shell script then run it weekly using cron so it will be done automatically.
 
It looks a good idea. I can't test it yet because I have already manually deleted the expired files. Will check tomorrow.

Cheers guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top