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

Removing files

Status
Not open for further replies.

sethu

Programmer
Apr 15, 2000
29
US
I have a directory in my sun solaris 7 machine which grows so rapidly. I want to remove some log files on a daily basis acoording to the date. What command can I use for that (selecting files according to date). Example I need to remove some files which has extension .log and created yesterday or day before. Can any of Solaris guru's help me?
 
You could try using the find command with the mtime (modification time) option and an exec to rm the file(s) when found. Something like (from the directory in which you're in when running the command):

find . -name '*.log' -mtime +2 -exec rm {} \;

would find .log files which haven't been modified for two days (-atime +2 would give you those which haven't been accessed for 2 days) and then delete them.

As with all rm commands, be very careful that the command will do exactly what you want and not cause unpleasant side-effects! See the find man page for further details. Good luck.

 
you can set it up so every day it finds the files and does a rm *.log. logs are usually store in var/logs

but once you rm the file you cannot get it back. unlike windows with the trash can.
 
Hi,

Your command will remove all files beyond that date which we give with mtime. I need to remove some files on a date basis for example yesterday's OR day before's. Can you help on that?

 
Hmm ... how are the files named? It is very difficult to use find (or any other command) to remove files for a particular day.

The way I would do this is to include some form of date in the filename, e.g. call them file1.20010410.log, then file2.20010410.log.

This way, if I want to remove all files from a particular day, all I need to do is rm *.20010410.log

There are other ways ... You could write a shell script which cuts the last modified date out of the ls -l command, compares that to the date you want to delete, and delete the file if required.

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top