Forum Moderators: coopster & phranque

Message Too Old, No Replies

Using 'test'- Finding age of symbolic link

         

bradsmith74

5:18 pm on Oct 26, 2004 (gmt 0)

10+ Year Member



I'm trying to find the age since modification of symbolic links using the 'test' syntax in perl. The problem is that when I do it, what is returned is the age of the file that the symbolic link is pointing to. The syntax that I'm using is this:


$dir = "/home/www/html/test/symlinks" ;

opendir (DIR, "$dir/");
@FILES = grep(/[a-zA-Z0-9]/,readdir(DIR));
closedir (DIR);

## PRINT SYMLINKS AND THEIR AGE
foreach $FILE (@FILES)
{
print ("$FILE - Size=".(-M "$dir/$FILE"));
}

Is there some way to get the age of the actual symlinks?

jollymcfats

4:46 am on Oct 27, 2004 (gmt 0)

10+ Year Member



lstat
will get you information about the link itself.

bradsmith74

6:26 am on Oct 27, 2004 (gmt 0)

10+ Year Member



jollymcfats- Thank you very much, that was exactly what I needed. In case anyone else is interrested, I was writing a script that removes symbolic links that are more than a few hours old. Here is the final script:

$dir = "/path/to/directory/symlinks" ;

opendir (DIR, "$dir/");
@FILES = grep(/[a-zA-Z0-9]/,readdir(DIR));
closedir (DIR);

foreach $FILE (@FILES) {

#check to make sure it's a symboic link
if (-l "$dir/$FILE") {

# see when it was last modified
$mtime = (lstat("$dir/$FILE"))[9];
$ageH = ( (time - $mtime) / 60 ) / 60;
if ( $ageH > 2 ) {
unlink("$dir/$FILE");
}
}
}