# # How old is that file?
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

How old is that file?

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© November 2004 Tony Lawrence

(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:



#!/usr/bin/perl
# howold.pl
$file=shift @ARGV;
@stat=stat $file;
$now=time();
$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=`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[0]))[10] )/60);
 

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


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





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

Printer Friendly Version

->
-> How old is that file?


Inexpensive and informative Apple related e-books:

Digital Sharing Crash Course

Take Control of OS X Server

iOS 10: A Take Control Crash Course

Take Control of Apple Mail, Third Edition

Take Control of the Mac Command Line with Terminal, Second Edition





More Articles by © Tony Lawrence








Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





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)




Linux posts

Troubleshooting posts


This post tagged:

Blog

Perl

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode