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

shell script to remove files by date

Status
Not open for further replies.

Ronfogg

Technical User
Oct 19, 2001
10
US
Hey there Shell experts!

Has anyone got a good script to perform a file removal by date? I need something that will wade through a LARGE reports directory, and delete everything more than 30 days old. I have ideas about what I could write, but am not sure about the redirect or pipe function that would filter the 'rm' or 'del' command through the date process. Any comments/help are MORE than welcome. Thanks!

Ron Fogg
Systems Manager
Waldo County General Hospital
 
i use the find command -

find <dir> -ctime +30 -name 'ora*.aud' -exec rm {} \;

this removes all files in <dir> that have not been changed for 30 days and match the pattern ora*.aud.

hope this helps
 
Ron, why write a script when find seems to do everything you require:

From the top level directory you want to remove files from, do:

find . -mtime +30 -print

to check that the files are those you wish to delete. If you're happy, and from the same location, do:

find . -mtime +30 -exec rm {} \;

to delete them. As always, be careful with any rm command, hence the checking stage above. If this isn't what you require, let us know.

Cheers.
 
[tt]
/# cd /bigdir
/bigdir# find . -mtime +30 -ls| more
[/tt]

Confirm that only the files you want to delete are listed. If there are files listed that you want to keep, you'll need spend some time with the find command documentation (worth the time anyway). Once you've got your find working the way you'd like, get rid of the pipe to more and add -exec rm.

As an example, if the find command listed above lists exactly the files that you want to delete, then your final command would look like this:

[tt]
/bigdir# find . -mtime +30 -exec rm {} \;
[/tt]



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Many Thanks to 'gbarth', 'KenCunningham', and 'RodKnowlton' for your inputs. I was thinking in far greater detail than I needed to. Your quick responses are greatly appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top