Tue Nov 2 16:16:41 2004 How old is
Posted by Tony Lawrence
Search Keys: shell
Sometimes you want to know the age of a file. Perl has a "-M" test that gives you age in days, but this customer needed it in minutes. That's easy:
#!/usr/bin/perl # howold.pl $file=shift @ARGV; @stat=stat $file; $now=time(); $mtime=sprintf("%d",($now -$stat)/60); # That's inode change time, stat would be modification, and 8 is access print "$mtime\n";
You'd use this in a script like this:
OLD=`howold.pl myfile` if [ "$OLD" -gt 20 ] then echo "older than 20 minutes" else echo "Younger" fi
You could put the whole script on one line:
printf("%d\n",(time() - (stat(@ARGV)) )/60);
but that's much harder to follow and a lot easier to screw up.
Got something to add? Send me email.
More Articles by Tony Lawrence © 2012-06-17 Tony Lawrence
It is not only that there is no hiding place for the gods from the searching telescope and microscope; there is no such society any more as the gods once supported. (Joseph Campbell)