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

Unix Script - Delete Files over 60 days Old 1

Status
Not open for further replies.

saw15

Technical User
Jan 24, 2001
468
0
0
US
Good day ..

New to Unix: I need to delete (on an ongoing basis) specific files in specific directories that are 61 days or older.

I would like to create a batch job, or script to accomplish the job, but have NO idea how.

I am looking for any example or assistance with what seems a simple job.

Thanks
 
You should be able to accomplish this with:

find <path to directory to clear> -mtime +60 -exec rm {} \;

this will clear the directory of ANY file which has remained unmodified for 60 or more days. Be careful, though, as any scripts etc which you may have in the same directory will be removed too. To avoid this, you could add a further option to the above:

-name '<filename>'

note that <filename> can be wildcarded (eg *.log), but if it is it must be enclosed in single quotes. Hope this helps.
 
Do you know how I would identify this to select only text &quot;*.txt&quot; files?

If that is possible.. hope so.

Thanks for the help though .. appreciate it greatly.
 
This should do the trick:

find <path to directory to clear> -name '*.txt' -mtime +60 -exec rm {} \;

Note that this assumes your over-60-days criteria still applies. If not, take out the -mtime +60 option. Good luck.

 
Look at the type option for find -
-> you can specify type -f
Also look at rm options
-> you can use rm -f
Tip: Always experiment with your find cmd before
executing with a cron job
 
Will this also working if you are on UNIX trying to delete from a lan directory?

I tried the find command above and get
find: cannot stat <my directory>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top