The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
deletion ofsunburntYux SunBurntYux Forum Support Area for Unregistered Users & Account Problems 0 06-02-2008 05:16 PM
/var/cache deletion. mjdousti UNIX for Advanced & Expert Users 1 05-01-2008 12:36 PM
Deletion of log files. Geeta UNIX for Dummies Questions & Answers 1 06-23-2006 07:04 AM
Regarding deletion of old files Chidvilas Shell Programming and Scripting 3 12-27-2005 10:05 PM
Account Deletion... spaceshiporion Post Here to Contact Site Administrators and Moderators 2 06-24-2005 07:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2007
srirams srirams is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
Exclamation Deletion of Logs

Can someone suggest on this script :

let's say the logs files are available for Jan 07, Dec 06, Nov 06, Oct 06

the script should identify the latest months logs, i.e Jan 07

it should then delete anything older than 2 months, which will be logs for Nov 06 & Oct 06.
  #2 (permalink)  
Old 02-21-2007
moshe fried moshe fried is offline
Registered User
  
 

Join Date: Feb 2007
Location: Israel
Posts: 24
Here is a script that you can use in perl based on the file's timestamp

#!/usr/bin/env perl


die "Syntax: $0 directory" if (@ARGV != 1 || ! -d $ARGV[0]);
$TwoMonths = 60 * 60 * 24 * 60; # Seconds in two months (+/-)
$Dir = $ARGV[0];
die "Can't open directory $Dir - $!" if (!opendir (DIR, $Dir));
map ($Filelist{"$Dir/$_"} = (stat ("$Dir/$_"))[9] , grep {-f "$Dir/$_"} readdir(DIR));
closedir (DIR);
# Latest file's timestamp in seconds
$LTS=$Filelist{$tmpfile = (sort{$Filelist{$b} <=> $Filelist{$a}} keys (%Filelist))[0]};
@FilesToDelete = grep ($TwoMonths < ($LTS - $Filelist{$_}), keys (%Filelist));
print "\nlast file is $tmpfile, $Filelist{$tmpfile}\nFiles to delete:". join ("\n", @FilesToDelete) . "\n";
# You can now remove files
unlink @FilesToDelete;
  #3 (permalink)  
Old 02-22-2007
srirams srirams is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
Checking on this
  #4 (permalink)  
Old 02-22-2007
gopidesaboyina gopidesaboyina is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 29
Try out this dude..

ls -ltr | grep -v "date you wanna keep" | awk '{print $NF}' | xargs -i rm -rf {}
  #5 (permalink)  
Old 02-23-2007
srirams srirams is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
Unhappy

file - script.sh

#!/bin/sh

#!/bin/bash
Keepdate="/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'"
/bin/ls -ltr | grep -v "$Keepdate" | awk '{print $NF}' | xargs -i rm -rf {}


The above script deleted Every thing:

bash-3.00$ ls -ult --full-time
total 8
-rwxrwxrwx 1 ss54066 user 164 2007-02-23 06:24:56.920724000 -0500 check.sh
-rwxrwxrwx 1 ss54066 user 72 2007-02-22 06:07:45.044181000 -0500 Command
-rw-r--r-- 1 ss54066 user 0 2007-02-12 00:00:00.000000000 -0500 touch
-rw-r--r-- 1 ss54066 user 7 2003-02-12 00:00:00.000000000 -0500 1.txt
bash-3.00$ ./script.sh
bash-3.00$ ls -ult --full-time
total 0

This should have deleted only 2003
  #6 (permalink)  
Old 02-23-2007
gopidesaboyina gopidesaboyina is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 29
Keepdate="/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'"

modify that to like this..u need to use carets not double quotes. then echo keepdate also to get the actual variable value
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'`
  #7 (permalink)  
Old 02-23-2007
srirams srirams is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
Thumbs down

hmmm ... should that delete only 2003 ?
Closed Thread

Bookmarks

Tags
linux, mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0