How old is that file?

(Traditional format)

Tue Nov 2 16:16:41 2004 How old is that file?
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:

$file=shift @ARGV;
@stat=stat $file;
$mtime=sprintf("%d",($now -$stat[10])/60);
# That's inode change time, stat[9] would be modification, and 8 is access
print "$mtime\n";

You'd use this in a script like this:

OLD=` myfile`
if [ "$OLD" -gt 20 ]
  echo "older than 20 minutes"
  echo "Younger"

You could put the whole script on one line:

printf("%d\n",(time() - (stat(@ARGV[0]))[10] )/60);

but that's much harder to follow and a lot easier to screw up.

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> How old is that file?

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us