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 07-06-2011, 06:04 AM   #1
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Rep: Reputation: Disabled
Script to delete logfiles older than 1 day


Hi...

I use this below script to delete the logfiles older than 1 day .

find /home/cie101/madhu1 -mtime +1 -exec rm {} \;

and it doesnot work properly.

Can someone help me out??
 
Old 07-06-2011, 06:19 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What does "does not work properly" mean? Any error message? Does it not give the expected results? An advice: first try the find command without the -exec rm part, otherwise you might accidentaly delete the wrong files: once you got the desired results, add the -exec rm (or -delete) and run again.
 
Old 07-06-2011, 06:31 AM   #3
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
ya thanks for that..i am jus trying in my home dir..so no issues..

This is how the target location looks like and i want to retain only july6th (current date) logs and delete the rest.


[root@ngmlx409 httpd]# ll
total 1258512
-rw-r--r-- 1 root root 18321624 Jul 5 00:59 ite04_access_log.2011-07-04-00_00_00
-rw-r--r-- 1 root root 18348550 Jul 6 00:59 ite04_access_log.2011-07-05-00_00_00
-rw-r--r-- 1 root root 7102807 Jul 6 10:14 ite04_access_log.2011-07-06-00_00_00
-rw-r--r-- 1 root root 711 Jul 5 10:54 ite04_error_log.2011-07-05-00_00_00
-rw-r--r-- 1 root root 60937596 Jul 5 00:59 ite04_rewrite_log.2011-07-04-00_00_00
-rw-r--r-- 1 root root 60877552 Jul 6 00:59 ite04_rewrite_log.2011-07-05-00_00_00
-rw-r--r-- 1 root root 23484268 Jul 6 10:14 ite04_rewrite_log.2011-07-06-00_00_00
-rwxr-x--- 1 root root 800 Jan 26 09:29 log_http_clear
-rw-r----- 1 root root 0 Jul 6 09:07 rewrite_log
-rw-r----- 1 root root 549158912 Jul 5 09:03 rewrite_log_5Jul.tar.gz
-rw-r----- 1 root root 549158912 Jul 6 09:07 rewrite_log_6Jul.tar.gz
-rw-r--r-- 1 root root 0 Jan 17 17:33 ssl_access_log
-rw-r--r-- 1 root root 0 May 19 18:10 ssl_error_log
-rw-r--r-- 1 root root 0 Jan 17 17:33 ssl_request_log


[root@ngmlx409 httpd]# pwd
/var/sitelogs/httpd



[root@ngmlx409 httpd]# find /var/sitelogs/httpd -mtime +1 -exec rm {} \;
[root@ngmlx409 httpd]# ll
total 1258508
-rw-r--r-- 1 root root 18321624 Jul 5 00:59 ite04_access_log.2011-07-04-00_00_00
-rw-r--r-- 1 root root 18348550 Jul 6 00:59 ite04_access_log.2011-07-05-00_00_00
-rw-r--r-- 1 root root 7102807 Jul 6 10:14 ite04_access_log.2011-07-06-00_00_00
-rw-r--r-- 1 root root 711 Jul 5 10:54 ite04_error_log.2011-07-05-00_00_00
-rw-r--r-- 1 root root 60937596 Jul 5 00:59 ite04_rewrite_log.2011-07-04-00_00_00
-rw-r--r-- 1 root root 60877552 Jul 6 00:59 ite04_rewrite_log.2011-07-05-00_00_00
-rw-r--r-- 1 root root 23484268 Jul 6 10:14 ite04_rewrite_log.2011-07-06-00_00_00
-rw-r----- 1 root root 0 Jul 6 09:07 rewrite_log
-rw-r----- 1 root root 549158912 Jul 5 09:03 rewrite_log_5Jul.tar.gz
-rw-r----- 1 root root 549158912 Jul 6 09:07 rewrite_log_6Jul.tar.gz

After executing the script ,i dont get the desired result..

Not sure wher it goes wrong..
 
Old 07-06-2011, 06:31 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
I saw the other post of yours (please don't duplicate posts, nor hijack existing threads) and looking at the timestamp of the files it's clear now that it fails because of the -atime/-mtime rule of the find command:
Code:
-atime n
      File was last accessed n*24 hours ago.  When find figures out how many 24-hour periods ago the
      file was last accessed, any fractional part is ignored, so to match -atime +1, a file  has  to
      have been accessed at least two days ago.
To find file modified more than 24 hours ago, you have to use -mitime +0. If the files have been modified 0.24 or 0.78 days ago (that is less than one day) the fractional part will be ignored and the files don't match the +0 condition. On the other hand, files modified 1.01 or 1.65 days ago will be treated as if they were modified 1 day ago, that is more than 0. Hope this helps.
 
Old 07-06-2011, 06:35 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by madhumithamohan View Post
This is how the target location looks like and i want to retain only july6th (current date) logs and delete the rest.
If you want to delete all the files created yesterday (even if they were last modified less than 24 hours ago, e.g. Jul 5 23.59) you have to add the -daystart option:
Code:
-daystart
     Measure  times  (for  -amin,  -atime,  -cmin, -ctime, -mmin, and -mtime) from the beginning of
     today rather than from 24 hours ago.  This option only affects tests which appear later on the
     command line.
 
Old 07-06-2011, 06:41 AM   #6
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
ok...if that thee case..

please look at this:
total 16
-rwxr-x--- 1 cie101 users 62 Jul 6 08:43 mad1
-rwxr-x--- 1 cie101 users 437 Jul 6 07:21 mad3
-rw-r----- 1 cie101 users 0 Jul 4 21:21 madd
-rwxr-x--- 1 cie101 users 24 Jul 5 10:10 madfile2
-rwxr-x--- 1 cie101 users 0 Jul 5 10:10 madfile3
-rw-r----- 1 cie101 users 13 Jul 6 11:23 mady
[cie101@ngmlx409 madhu1]$ find /home/cie101/madhu1 -type f -mtime +1 -exec rm -f {} \;
[cie101@ngmlx409 madhu1]$ ll
total 16
-rwxr-x--- 1 cie101 users 62 Jul 6 08:43 mad1
-rwxr-x--- 1 cie101 users 437 Jul 6 07:21 mad3
-rw-r----- 1 cie101 users 0 Jul 4 21:21 madd
-rwxr-x--- 1 cie101 users 24 Jul 5 10:10 madfile2
-rwxr-x--- 1 cie101 users 0 Jul 5 10:10 madfile3
-rw-r----- 1 cie101 users 13 Jul 6 11:23 mady
[cie101@ngmlx409 madhu1]$


In this case...June 4th file must be deleted right??
 
Old 07-06-2011, 06:45 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by madhumithamohan View Post
In this case...June 4th file must be deleted right??
Only if the current time in your country is 21:21 or later, for the reasons explained above.
 
Old 07-06-2011, 06:55 AM   #8
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
ok...i am getting confused with the timestamp stuff rightnow.can you help me with the post #3...
 
Old 07-06-2011, 07:04 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Note that you can also use "-delete" instead of "-exec rm {} \;".
 
Old 07-06-2011, 07:12 AM   #10
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
@post 9

is that the syntax

[cie101@ngmlx409 madhu1]$ find /home/cie101/madhu1 -type f -mtime +1 -delete
find: invalid predicate `-delete'

[cie101@ngmlx409 madhu1]$ find /home/cie101/madu1 -type f -mtime +1 -delete \;
find: invalid predicate `-delete'

Not working...
 
Old 07-06-2011, 07:12 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
It actually remove the following (older) files, right?
Code:
-rwxr-x--- 1 root root 800 Jan 26 09:29 log_http_clear
-rw-r--r-- 1 root root 0 Jan 17 17:33 ssl_access_log
-rw-r--r-- 1 root root 0 May 19 18:10 ssl_error_log
-rw-r--r-- 1 root root 0 Jan 17 17:33 ssl_request_log
But you used -mtime +1 so that all the files modified more than 47 hours and 59 minutes ago where found (and deleted). If you want to find all the files modified more then 23 hours and 59 minutes ago, you have to use
Code:
-mtime +0
because -mtime treats all the decimal values 0.1, 0.2, 0.3 ... 0.9999 as 0! The condition is +0, that is more than 0 and matches only files modified 1.0000 days ago (or more). If you use +1 it matches files modified 2.0000 days ago (or more) and not yesterday!

Again, if you want to delete all the files created on 5th Jul (even if they were created less than 24 hours ago, for example yesterday night at 23.59) you have to use the -daystart option.

Now try the following:
Code:
find /var/sitelogs/httpd -mtime +0 -exec ls -l {} \;
and look at the difference from the output of:
Code:
find /var/sitelogs/httpd -daystart -mtime +0 -exec ls -l {} \;
After that it should be a little more clear (hopefully).
 
Old 07-06-2011, 08:23 AM   #12
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks a lot for your help..

The reason for which i delete those files is tht the file system gets full and it generates a ticket.So manually i keep deleting it daily,...according to post 3....i have to delete most of the 5th files and retain the 6th...so it looks fine if i use "find /var/sitelogs/httpd -daystart -mtime +0 -exec ls -l {} \;"

But in worst case if we are in need of the 5th supposingly if 6th dint arrive..then taht ll be a problem...So i thought why cant we move the files we grep'd to a new location (say my home dir).In this case the i will always have a backup rite?

I know th eworking of Move (mv) command ...but could you help me out in implementing in my scenario(post 3)?
 
Old 07-06-2011, 08:59 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can first check if logfiles of today are available, then remove the older ones. I mean for example:
Code:
if [[ $(find /var/sitelogs/httpd -daystart -type f -mtime 0 | wc -l) -ne 0 ]]
then
  find /var/sitelogs/httpd -daystart -type f -mtime +0 -exec echo rm {} \;
fi
Note the difference between -mtime 0 and -mtime +0.
Anyway, given your requirement you might try to use logrotate, which is more suitable for this kind of task. For example you can establish a rule to rotate all the logs inside /var/sitelogs/httpd on a daily basis and keep only those ones of the last two days, removing the rest.
 
  


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
Rotate logfiles multiple times a day jonaskellens Linux - Newbie 7 03-13-2011 03:25 PM
script to compress a directory and then delete older than 7 days replica88 Linux - General 3 02-03-2010 08:21 PM
Script to delete older files not running . linuxlover.chaitanya Linux - Newbie 10 01-12-2009 02:27 AM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 06:46 AM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:33 AM.

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