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 |
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 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 |
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 |
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. |
You can try two consecutive actions using -exec, e.g.
Code:
find . -mtime +10 -exec zip archive.zip {} \+ -exec rm {} \+ Code:
find . -mtime +10 -exec zip -g archive.zip {} \; -delete |
Quote:
-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) |
All times are GMT -5. The time now is 09:07 PM. |