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

Remove Old Files 2

Status
Not open for further replies.

amacbean

Programmer
Nov 18, 2002
9
US
Can anyone advise me on the best method deleting files that are over a certain age?

I am writing a script that needs to delete all files that are over 1 month old?

Any help would be great!
 
Code:
find . -mtime +31 -exec rm {} \; -print

everything older than 31 days will be deleted from the current directory down.

it will leave directories intact.
 
It's probably easiest to use find as follows:

find </toplevel_directory> -type f -mtime +30 -exec rm {} \;

This traverses through your directory structure and deletes all files over 30 days old. To get an idea as to what it is going to delete, it may be best to replace the -exec rm {} \; with a simple -print command first, as it's all too easy to set this off in the wrong place and delete the wrong files! HTH.

 
I'm just a rookie here, but don't you want a &quot;-f&quot; after the rm, so that it won't try to prompt you for each deletion?

As a side note, why not do this as a crontab entry, rather than a script? I run a crontab entry on my machines that runs every night at midnight and deletes any tiff files in a certain directory that are more than one day old. It looks like this:

0 0 * * * find /var/spool/drop_box/images -name &quot;*TIF&quot; -mtime +1 -exec rm -f {} \;

It could certainly be modified to suit your needs.

Allen
 
if you're doing it at the prompt you might have set read-only on purpose so that you don't delete files from certain directories by accident.

if you had done that you wouldn't want -f option in rm.

the cron entry is useful though if you want the files removed automatically.
 
Thanks for the clarification, I hadn't considered that.

Allen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top