LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-22-2012, 11:36 AM   #1
sarathicse
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
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
 
Old 10-22-2012, 02:06 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
Old 10-22-2012, 03: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: Reputation: 286Reputation: 286Reputation: 286
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 03:18 PM.
 
Old 10-25-2012, 06:15 AM   #4
sarathicse
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-25-2012, 06:30 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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).
 
Old 10-25-2012, 01: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: Reputation: 286Reputation: 286Reputation: 286
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 01:52 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help to have a rsync script log output to a file with time stamp Thaidog Programming 5 11-15-2011 06:37 PM
time stamp of files ZAMO Linux - General 1 12-17-2008 06:45 AM
Rename file based on its own date/time stamp airman99 Linux - General 19 09-05-2006 08:52 AM
search based on time stamp ? massai Linux - Newbie 1 03-07-2004 03:50 PM
Time stamp in Samba is 11 hours behind time stamp in Linux Linh Linux - General 3 09-04-2003 01:44 PM


All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration