![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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; |
|
||||
|
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 }'` |
![]() |
| Bookmarks |
| Tags |
| linux, mtime |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|