Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Need to delete log files based on Time Stamp
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 10-22-2012, 10:36 AM #1 sarathicse LQ Newbie   Registered: Oct 2012 Posts: 2 Rep: Need to delete log files based on Time Stamp Hello Guys, I am having log files in location \home\sarathi\sarathi. The format of the files are such a way that it will be sarathi_2012_10_22, if the file was for 22nd October. I need a command or script to delete all the log files that have timestamp older than 10 days. Also is it possible to have a command so that the files that are gonna be deleted will be zipped as a file. For deleting using date i know a command find /home/sarathi/sarathi/* -mmin +1 -exec rm -f {} \; But using timestamp is a little difficult for me... I am new to linux......Please assist. Thanks, Sarathi Kamaraj
 10-22-2012, 01:06 PM #2 colucix LQ Guru   Registered: Sep 2003 Location: Bologna Distribution: CentOS 6.5 OpenSuSE 12.3 Posts: 10,509 Rep: Hi and welcome to LinuxQuestions! Most likely the modification time of your logs is the same as the date in the file name, therefore you can use the find command with -mtime (not -mmin as in your example): Code: find /home/sarathi/sarathi -mtime +10 -delete warning: test this command without -delete to prevent unwanted deletions, until you're sure of the result of the find command. If you still want to remove older files based on the date in the filename, you can take advantage of the fact that the names are sorted alpha-numerically. This is because of the yyyy-mm-dd format of the date (it wouldn't be true for example if you had dd-mm-yyyy), therefore something like: Code: cd /home/sarathi/sarathi for file in sarathi_????_??_?? do if [[ $file <$(date -d "10 days ago" +sarathi_%Y_%m_%d) ]] then zip -g archive.zip $file && rm$file fi done should do the trick. Note the -g option of the zip command adds a file to an existing archive and prints out a warning if the archive doesn't exist yet, but it should be created properly. 1 members found this post helpful.
 10-22-2012, 02:13 PM #3 shivaa Senior Member   Registered: Jul 2012 Location: Grenoble, Fr. Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0 Posts: 1,800 Blog Entries: 4 Rep: I won't give you a ready-made script, but will show you a possible way: Create a script and define following variables and code: Code: #!/bin/bash date=$(date +%d) month=$(date +%m) year=$(date +%Y) prevdate=$(date --date="10 day ago" | cut -d" " -f3) ## 10 days old date mkdir /home/sarathi/oldfiles ls -la $HOME/sarathi\_$year\_$month\_$prevdate* | cut -d" " -f9 > /tmp/listoffiles.txt for i in cat /tmp/listoffiles.txt do mv $i /home/sarathi/oldfiles/ done tar -zcvf oldfilesbckup.$year\_$month\_$date.tar.gz /home/sarathi/oldfiles/* \rm -rf /home/sarathi/oldfiles/ Hope it will help! Last edited by shivaa; 10-22-2012 at 02:18 PM.
 10-25-2012, 05:15 AM #4 sarathicse LQ Newbie   Registered: Oct 2012 Posts: 2 Original Poster Rep: Thank you both for your reply... Is there a way to get this find /home/sarathi/sarathi -mtime +10 -delete and Zip in a single command. I tried something like find /home/sarathi/sarathi -mtime +10 zip -g archive.zip -delete but didnt get the desired output. Also Shivaa thanks for your post. It helped me extend my knowledge of understanding the Zip using Tar. Thanks in advance.
 10-25-2012, 05:30 AM #5 colucix LQ Guru   Registered: Sep 2003 Location: Bologna Distribution: CentOS 6.5 OpenSuSE 12.3 Posts: 10,509 Rep: You can try two consecutive actions using -exec, e.g. Code: find . -mtime +10 -exec zip archive.zip {} \+ -exec rm {} \+ or Code: find . -mtime +10 -exec zip -g archive.zip {} \; -delete The former acts on all the files together and performs a single zip and a single rm command; the latter acts on one file at a time (see the difference between \; and \+ in man find).
10-25-2012, 12:50 PM   #6
shivaa
Senior Member

Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep:
Quote:
 I tried something like find /home/sarathi/sarathi -mtime +10 zip -g archive.zip -delete but didnt get the desired output.
You're using a wrong find command. You can't pass command like this without -exec option.
-mtime option will show you all files modified after or between a particular time, but not the accurate ones which you want. On the other hand, using the pattern i.e. date, year, predate, I mentioned my last comment, will give you the accurate list of file. In your case, the problem is how to list out correct file, not what action to be taken on them.
Anyway, if you want to do it without using any script then, use -exec option with find with following sequence of cmds:
Code:
# prevdate=$(date --date="10 day ago" | cut -d" " -f3) # month=$(date +%m)
# year=$(date +%Y) # export prevdate months year # find$HOME -name "sarathi\_$year\_$month\_$prevdate*" -exec ls -la {} \; # find$HOME -name "sarathi\_$year\_$month\_$prevdate*" -exec mv$HOME/oldfiles {} \;
# tar -zcvf oldfilesbckup.$year\_$month\_\$date.tar.gz  /home/sarathi/oldfiles/*
# \rm -rf /home/sarathi/oldfiles/

Last edited by shivaa; 10-25-2012 at 12:52 PM.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Thaidog Programming 5 11-15-2011 05:37 PM ZAMO Linux - General 1 12-17-2008 05:45 AM airman99 Linux - General 19 09-05-2006 07:52 AM massai Linux - Newbie 1 03-07-2004 02:50 PM Linh Linux - General 3 09-04-2003 12:44 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:08 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -