To delete logs older than 30 days | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

To delete logs older than 30 days

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-22-2011
mmunir mmunir is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 22 September 2011, 4:16 AM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
To delete logs older than 30 days

I want to write a shell script that deletes all log files in a directory that are older than 30 days except for 3 files:

I am using the following command:

Code:
find /tmp/logs -name "*.log" -mtime +30 -exec rm -f {} \;

But this command deletes all the log files.

How can i modify this script that all logs are deleted except for following files:

1-SQLUpdate.log
2-updated_days.log
3-qadirectsvcd.log

Moderator's Comments:
Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 09-22-2011 at 04:31 AM..
Sponsored Links
    #2  
Old 09-22-2011
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 19 April 2014, 1:02 AM EDT
Posts: 3,044
Thanks: 33
Thanked 653 Times in 632 Posts
execute the below command. and check whether it ignores the mentioned files.


Code:
 
find /tmp/logs -name "*.log" -mtime +30 -a ! -name "SQLUpdate*" -a ! -name "updated_days*" -a ! -name "qadirectsvcd*"

If you are satisfied with the output, then add the -exec part
Sponsored Links
    #3  
Old 09-22-2011
jayan_jay's Avatar
jayan_jay jayan_jay is offline Forum Advisor  
Forum Advisor
 
Join Date: Jul 2008
Last Activity: 2 December 2013, 5:52 PM EST
Posts: 832
Thanks: 9
Thanked 185 Times in 176 Posts

Code:
$ find /tmp/logs ! \( -name SQLUpdate.log -o -name updated_days.log -o -name qadirectsvcd.log \) -mtime +30 -exec rm {} \;

    #4  
Old 09-22-2011
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 19 April 2014, 1:02 AM EDT
Posts: 3,044
Thanks: 33
Thanked 653 Times in 632 Posts
jayan you missed the -name "*.log"

otherwise it will delete other extension files also
Sponsored Links
    #5  
Old 09-22-2011
jayan_jay's Avatar
jayan_jay jayan_jay is offline Forum Advisor  
Forum Advisor
 
Join Date: Jul 2008
Last Activity: 2 December 2013, 5:52 PM EST
Posts: 832
Thanks: 9
Thanked 185 Times in 176 Posts
Yup .. missed it due to sleepless nights Thanks ..
Sponsored Links
    #6  
Old 09-22-2011
mmunir mmunir is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 22 September 2011, 4:16 AM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks to all of you for the help. Following is the final command:


Code:
find . -name "*.log" -mtime +30 -a ! -name "SQLUpdate.log" -a ! -name "updated_days*" -a ! -name "qadirectsvcd*" -exec rm -f {} \;

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete folders older than 30 days joneggk Shell Programming and Scripting 2 04-08-2010 10:49 AM
How do i delete files older than 15 days in AIX? bluesteel AIX 4 03-31-2010 11:23 AM
delete files more than 15 days older ali560045 Shell Programming and Scripting 3 11-28-2008 04:43 AM
how to archive logs older than 5 days & then delete them? timus1980 UNIX for Advanced & Expert Users 1 02-08-2008 05:53 AM
delete files older than 7 days lesstjm UNIX for Dummies Questions & Answers 1 11-06-2001 09:43 AM



All times are GMT -4. The time now is 10:02 PM.