File Date Comparison

    March 21, 2006

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 keeo 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: as noted while casting shame upon me and others who carelessly referred to ctime as creation time, 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 “”) 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:


*Originally published at

Add to | DiggThis | Yahoo! My Web


A.P. Lawrence provides SCO Unix and Linux consulting services