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

Delete old files in directory

Status
Not open for further replies.

Vandy02

Programmer
Jan 7, 2003
151
US
I am wanting look in a directory...as in a current directory...let us say \COMPANY

Within this directory have multiple files listed by date such as
test20040802.Z
test20040803.Z
test20040804.Z

I want to be able to delete the files that would have date being greater than sysdate mine 100 days..
In other words, if the file was test20040101.Z this script would delete the file...I am using Bourne Shell...

Thanks
 
If the date of the files is correct, you could use a simple find-command:

Code:
find ./ -mtime +100 -exec rm {} \;
or with the directory-name
Code:
find /here/is/Company -mtime +100 -exec rm {} \;

Of course you check the command by first trying 'ls -la' instead of 'rm', because 'find' is a sharp knive.

If the date-of-file doesn't match it's name, I would consider using '3 months' instead of 100 days.

seeking a job as java-programmer in Berlin:
 
i am unable to do so..

find ./ -type f -name "test*" -mtime +30 -exec rm {}
without -exec I return the files I need

when I add th -exec i get

find: missing argument to 'exec'
 
Replace this:
find ./ -type f -name "test*" -mtime +30 -exec rm {} By this:
find ./ -type f -name "test*" -mtime +30 -exec rm {} \[highlight];[/highlight]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yep!

That worked.
Thanks for the help!
 
Perhaps check the man page for your version of find. You might be able to use "[highlight]+[/highlight]" instead of "[highlight];[/highlight]".
The end of cmd must be punctuated by a semicolon (;) or a plus sign (+) (semicolon and plus are special to the shell and must be escaped). When a plus sign is used, cmd aggregates a set of pathnames and executes on the set. The reason for preferring + to a semicolon is vastly improved performance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top