Deletion of Logs

You copy the datacalc script to your system.

From with in you script you next can call this script like you call any other command or script.

Again, Suppose your most recent log file is from Jan 28, 2007

In your script:

JDATE=`<path to location of datacalc>/datacalc -j 2007 01 28`

The variable JDATE will now hold the Julian Date representation of Jan 28, 2007.

A Julian date is a single number

Next you substract the 60 day.

((JDATE=${JDATE}-60))

You connvert the Julian Date back to the regualar date format

REGDATE=`<path to location of datacalc>/datacalc -j ${JDATE}`

The variable REGDATE will now hold the string "2006 11 29"

#!/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;

Checking on this

Try out this dude..

ls -ltr | grep -v "date you wanna keep" | awk '{print $NF}' | xargs -i rm -rf {}

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

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 }'`

hmmm ... should that delete only 2003 ?

#!/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 {}

wont help ... it deletes all 2003 timestamp files and 2007 along with the script

in my case the directories are /opt/zensd

Heres where I want to find the latest timestamp and compare it with files which are more than 2 months older to the latest one and delete it

Should I make any changes to existing script :slight_smile:

Thanks

give me out put of this..

#!/bin/bash
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'`

echo $Keepdate

#/bin/ls -ltr | grep -v "$Keepdate" | awk '{print $NF}' | xargs -i rm -rf {}

bash-3.00$ cat test.sh
#!/bin/bash
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'`

echo $Keepdate

#/bin/ls -ltr | grep -v "$Keepdate" | awk '{print $NF}' | xargs -i rm -rf {}

bash-3.00$

bash-3.00$ ls -la
total 20
drwxr-xr-x 2 ss54066 user 4096 Feb 23 09:27 .
drwxr-x--- 51 ss54066 games 8192 Feb 20 08:48 ..
-rw-r--r-- 1 ss54066 user 0 Feb 12 2003 1.txt
-rwxrwxrwx 1 ss54066 user 690 Feb 23 08:49 test.pl
-rwxrwxrwx 1 ss54066 user 182 Feb 23 09:27 test.sh

bash-3.00$ ./test.sh
2007-02-23

bash-3.00$ ls -ult --full-time
total 8
-rwxrwxrwx 1 ss54066 user 182 2007-02-23 09:27:49.352858000 -0500 test.sh
-rwxrwxrwx 1 ss54066 user 690 2007-02-23 08:49:42.585992000 -0500 test.pl
-rw-r--r-- 1 ss54066 user 0 2003-02-12 00:00:00.000000000 -0500 1.txt
did not delete anything

it won't delete becuase i commented out rm. try this..btw..what is the unix version do u use ?

#!/bin/bash
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'`

echo $Keepdate

/bin/ls -ult --full-time | grep -v "$Keepdate" | awk '{print $NF}' | xargs -i rm -rf {}

I am running this on linux

$uname -a
Linux hostname.lax 2.6.9-22.ELsmp #1 SMP Mon Sep 19 18:00:54 EDT 2005 x86_64 x86_
64 x86_64 GNU/Linux

The Script File that I run

bash-3.00$ cat script.sh
#!/bin/bash
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'`

echo $Keepdate

/bin/ls -ult --full-time | grep -v "$Keepdate" | awk '{print $NF}' | xargs -i rm
-rf {}

Output of ls before I run script
bash-3.00$ ls -ult --full-time
total 16
-rwxrwxrwx 1 ss54066 user 193 2007-02-26 05:03:44.684353000 -0500 script.sh
-rwxrwxrwx 1 ss54066 user 182 2007-02-23 09:27:49.352858000 -0500 test.sh
-rwxrwxrwx 1 ss54066 user 690 2007-02-23 08:49:42.585992000 -0500 test.pl
-rw-r--r-- 1 ss54066 user 4060 2004-02-21 02:33:22.000000000 -0500 1.txt

I run the Script now
bash-3.00$ ./script.sh
2007-02-26

ls files output - After I run the script

bash-3.00$ ls -ult --full-timetotal 4
-rwxrwxrwx 1 ss54066 user 193 2007-02-26 05:03:44.684353000 -0500 script.sh
bash-3.00$

guys,

any updates

Hmmm..looks strange to me..anyways give me output of this..

#!/bin/bash
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }' | awk -F"-" '{print $1}'`-

echo $Keepdate

/bin/ls -ult --full-time | grep -v "$Keepdate"

I need to keep files for X days from latest date (not just one day)

I need to keep files for X days from latest date (not just one day)
on a linux system :slight_smile:

is it possible to keep files for X days from latest date ???