Deletion of Logs


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Deletion of Logs
# 29  
Old 02-23-2007
Question

wont help ... it deletes all 2003 timestamp files and 2007 along with the script
# 30  
Old 02-23-2007
Quote:
Originally Posted by moshe fried
#!/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;
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 Smilie

Thanks
# 31  
Old 02-23-2007
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 {}
# 32  
Old 02-23-2007
Java

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
# 33  
Old 02-23-2007
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 {}
# 34  
Old 02-26-2007
Question

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
# 35  
Old 02-26-2007
Bug

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$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Post Here to Contact Site Administrators and Moderators

Post deletion

Admin: I've had posted twice my last post Can you please delete it from the SCO forum=? thanks (1 Reply)
Discussion started by: funyotros
1 Replies

3. Shell Programming and Scripting

advanced deletion

How can i delete the contents of the directory except one file? (8 Replies)
Discussion started by: proactiveaditya
8 Replies

4. Shell Programming and Scripting

Deletion of records

Hi, I have file with footer (5records) at end of the file. I want to delete the footer as well as the empty records between the main records and footer record.(The gap between main records and Footer records is dynamic) Example: MainRecord1 "115494",","FAELD","CT","... (12 Replies)
Discussion started by: vsairam
12 Replies

5. Shell Programming and Scripting

Deletion of a particular character

How to delete a particular character in a file and then removing the spaces created by that removal.I am working with a text file containing nucleotide sequences. I have tried sed command but it replaces N with spaces. example:I want to delete all the N without creating a space after its ... (1 Reply)
Discussion started by: ankitachaurasia
1 Replies

6. Shell Programming and Scripting

Grep yesterday logs from weblogic logs

Hi, I am trying to write a script which would go search and get the info from the logs based on yesterday timestamp and write yesterday logs in new file. The log file format is as follows: """"""""""""""""""""""""""... (3 Replies)
Discussion started by: harish.parker
3 Replies

7. Forum Support Area for Unregistered Users & Account Problems

deletion ofsunburntYux

Could you please close SunburntYux as I would like to use the emaill address on this account to register FloridaBSD, (0 Replies)
Discussion started by: SunBurntYux
0 Replies

8. Shell Programming and Scripting

Help: deletion of record

I have created a address book file. Insertion of Record (using >> symbol)and searching of record (using grep command) into the address book is also working correctly. Now I want to delete a record into a file. Any body can help me in this case without using sed and awk18 18. Thanks and best... (24 Replies)
Discussion started by: murtaza
24 Replies

9. Shell Programming and Scripting

Regarding deletion of old files

Hi, I have a list of directories which contain old files that are to be deleted. I put the list of all directories in a txt file and it is being read by a script Iam searching for the files older than 60 days using mtime and then deleting it But all the files are getting deleted... (3 Replies)
Discussion started by: Chidvilas
3 Replies

10. Post Here to Contact Site Administrators and Moderators

Account Deletion...

Please delete my account? Thanks, so very much, really. (2 Replies)
Discussion started by: spaceshiporion
2 Replies
Login or Register to Ask a Question