LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to delete log files based on Time Stamp (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-delete-log-files-based-on-time-stamp-4175433512/)

sarathicse 10-22-2012 10:36 AM

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

colucix 10-22-2012 01:06 PM

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.

shivaa 10-22-2012 02:13 PM

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!

sarathicse 10-25-2012 05:15 AM

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.

colucix 10-25-2012 05:30 AM

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).

shivaa 10-25-2012 12:50 PM

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/



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