Link to home
Start Free TrialLog in
Avatar of Vahan Yerkanian
Vahan YerkanianFlag for Armenia

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/434323.zip
/base/a.b.c.d/e.z.x.y/655653.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.

Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you checking for the age of the symlink or the file that it links to? If it's the actual file use "stat" as shown below, if it 's the symlink replace it with "lstat". This code is untested, but I'm sure it's pretty close :)

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...
Avatar of Vahan Yerkanian

ASKER

i need to check the age of the symlink...

make your code tested, and get your 500 points =)
I also need to have a list of symliks deleted printed...
ASKER CERTIFIED SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.
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