APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

File date comparison

Sometimes you want to use the date of a file somewhere else. For this example, we'll use the case where a file shouldn't be overwritten if it was created or changed today.

If we are not entirely sure of the parameters, we should stop right there: what does "today" mean? Does it mean anytime after midnight of the day before, or does it mean within the past 24 hours? I have seen "today" actually mean the latter more than once, so keep that in mind when people are loosely tossing around "today".

We also should be asking which file time we need to work with. Is the creation time, the access time, or the modified time? When talking to non-programmers, even that isn't enough, because the "creation" time might be misinterpreted; if they opened an existing file and replaced its contents, they may think of that as "creation" rather than modification.

But for today, we'll keep it simple. We have a file, and it really is the creation date we want to look at, and "today" really is anything after midnight yesterday. We want to know if the file was created today.

By the way, I got yelled at for forgetting to mention that "ctime" is "changed time", not "creation time". More precisely, it's the inode change time. If you chmod a file, ctime changes. If you have not done anything to change the inode, ctime is creation time.

But some filesystems track four times, and one actually is the real creation time. FreeBSD's inode includes a "btime" (birth time) holder. It's still somewhat useless, as most utilities are unaware of the "birth time" even if the file system does support it. But we digress:

If we are fortunate enough to be working on a Linux platform, we have the "stat" command available. It's worth reading up on "stat"'s man page, but there are a couple of easy ways to get the creation date. Let's say our file is "z" and try a few things:

$ ls -l z
-rw-r--r--   1 apl  staff  2552 Sep  4 16:08 z
$ stat z
234881029 1802523 -rw-r--r-- 1 apl staff 0 2552 "Sep  5 13:54:27 2005" "Sep  4 16:08:15 2005" "Sep  4 16:08:15 2005" 4096 8 0 z
$ stat -f "%Sc" z      
Sep  4 16:08:15 2005

If we don't have stat, the shell becomes trickier because "ls -l" outputs differently depending on the modification time of the file; if it is more than 6 months in the past (or future), then the year of the last modification is displayed in place of the hour and minute fields. That forces us to look at the field to see if it has a ":" in it. Something like this will do it:

set -- `ls -l $1`
thisyear=`date "+%Y"`
case $8 in
   *:*) year=$thisyear;;
echo $year

But I will have switched to Perl long before I'd get into that ugly mess. Perl has "stat", and we'd use it like this:

$rfile=shift @ARGV;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat $rfile;
($sec, $min, $hour, $mday, $mon, $year) = localtime();
$today="$mday $mon $year";
($sec, $min, $hour, $mday, $mon, $year) = localtime($ctime);
$filedate="$mday $mon $year";
exit 1 if ($filedate eq $today);
exit 0;

You could use something like that in a shell script like this (assume the Perl script is called "filedate.pl")

filedate.pl filetocheck || exit 1

Obviously the "stat" gives us much more than we need here. If all those unused variables annoy you, use an array slice:


Got something to add? Send me email.


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Wed Nov 30 23:07:58 2005: 1391   BigDumbDinosaur

If we don't have stat, the shell becomes trickier because "ls -l" outputs differently...

ls -lT causes all file dates to be displayed in a consistent format.

We also should be asking which file time we need to work with.

Also the time zone could matter.

...and "today" really is anything after midnight yesterday.

Er...anything after midnight yesterday is yesterday until midnight of today -- a day starts at midnight, right? <Smile> So the question becomes one of whether a day boundary is at 12:00 AM in the local time zone, 12:00 AM in the user's time zone, the machine's time of day minus 23 hours, 59 minutes and 59 seconds, or the user's time of day minus 23 hours, 59 minutes and 59 seconds. Nothing like the time of day to make things confusing!

Wed Nov 30 23:20:51 2005: 1393   TonyLawrence

Not all "ls" will display full times with T (and those that do probably have stat). Some "ls" use -T for tab columns.

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