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

Delete files according to timestamp of file...

Status
Not open for further replies.

steela

Programmer
Sep 2, 2003
6
BE
Hi,

I'd like to remove a block of files according to their last update timestamp

e.g. rm -f *.log | datetime<01-JAN-2003

or something like that...

Thanks,

Aimee

 
You can use find to do this, provided that you know the number of days before which you want files deleted, using the -mtime +<No. of days> option, for example, to delete those files last modified more than 30 days ago:

find /<path to files> -mtime +30 -exec rm {} \;

Note that it is good practice to issue a -print command before executing the rm option, just to make sure that you're picking up only the files you want to delete and no others:

find /<path to files> -mtime +30 -print

Hope this helps.
 
Hi Aimee
find is you friend :
find curr_dir -name &quot;*filename*&quot; -mdate +350

will find files whose names are like 'filename' and are older than 350 days, in the current directory and all directories below it.
When you're happy you've got the correct file selection,
issue the same command with -exec rm {} \; after it, to delete 'em
find curr_dir -name &quot;*filename*&quot; -mdate +350 -exec rm {} \;

Dickie Bird (:)-)))
 
According to your requirement, I think that you wanted to remove the files according to the timestamp. Just wrote a simple script for you. If the script work, then you remove the '#' before command 'rm'.

for i in `ls -1 *.log`
do
logfile=`ls -l $i | awk '{print $6,$7,$9}'`
if [[ ${logfile% *} = 'Jan 1' ]];
then
echo &quot;Removing ${logfile##* }&quot;
#rm ${logfile##* }
else
echo &quot;Fail to remove ${logfile##* }&quot;
fi
done

tikual
 
You can touch a temporary file with the desired timestamp and then do a find . ! -newer .
Something like this:
Code:
touch -t 200301010000 /tmp/ref.tmp
find /path/to/logdir -name '*.log' ! -newer /tmp/ref.tmp | xargs rm

Hope This Help
PH.
 
Thanks guys - I think I can save a lot of space on our file-systems knowing this!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top