Vahan Yerkanian
asked on
Efficient symlink removal script
Hello Perlers,
Anyone can supply me with a script that checks a directory hierarchy full of symlinks and delete those that have last modified (done by touch) date older than X hours? This is gonna be a cron job, and the server is kind of high of cpu and i/o load, so the removal process should be maximally stressless.
500 points who gives me ready script.
directory hierarchy is as follows:
/base/a.b.c.d/232323.zip
/base/a.b.c.d/232324.zip
/base/a.b.e.f/345323.zip
/base/a.b.c.d/e.f.g.h/4343 23.zip
/base/a.b.c.d/e.z.x.y/6556 53.zip
a.b.c.d - random ip addresses, a.b.c.d/e.z.x.y - for users behind proxies. :)
all zips are symlinks to zips located somewhere else.
Anyone can supply me with a script that checks a directory hierarchy full of symlinks and delete those that have last modified (done by touch) date older than X hours? This is gonna be a cron job, and the server is kind of high of cpu and i/o load, so the removal process should be maximally stressless.
500 points who gives me ready script.
directory hierarchy is as follows:
/base/a.b.c.d/232323.zip
/base/a.b.c.d/232324.zip
/base/a.b.e.f/345323.zip
/base/a.b.c.d/e.f.g.h/4343
/base/a.b.c.d/e.z.x.y/6556
a.b.c.d - random ip addresses, a.b.c.d/e.z.x.y - for users behind proxies. :)
all zips are symlinks to zips located somewhere else.
ASKER
i need to check the age of the symlink...
make your code tested, and get your 500 points =)
make your code tested, and get your 500 points =)
ASKER
I also need to have a list of symliks deleted printed...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great.
if you add the following functionality, you'll get 600 points instead of 500.
I also need to delete subdirectories up to the $dir if the file deleted was the last one in the particular subdir
in other words, if /base/a/c contains only aa.zip and it's outdated, delete the file, rmdir /base/a/c, if there are no more files/directories in /base/a, delete /base/a
if you add the following functionality, you'll get 600 points instead of 500.
I also need to delete subdirectories up to the $dir if the file deleted was the last one in the particular subdir
in other words, if /base/a/c contains only aa.zip and it's outdated, delete the file, rmdir /base/a/c, if there are no more files/directories in /base/a, delete /base/a
Tell you what. Give me the 500 points for what I've done so far and post added functionality as a new question for 100 points.
ASKER
davorg,
You need to believe people more...
this is the new question: https://www.experts-exchange.com/questions/20725618/Efficient-symlink-removal-script-2.html
You need to believe people more...
this is the new question: https://www.experts-exchange.com/questions/20725618/Efficient-symlink-removal-script-2.html
use File::Find;
my $now = time;
my $hours = 10; # how old a file gets before it is deleted
my $age = $hours * 60 * 60; # $hours in seconds
my $dir = '/base'; # where to start from
find(\&wanted, $dir);
sub wanted {
next unless -l; # only handle symlinks
my $file_age = (stat )[8];
unlink if $file_age < $now - $age);
}
hth,
Dave...