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

File date comparison

© November 2005 Anthony Lawrence

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.

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

Printer Friendly Version

-> File date comparison


Inexpensive and informative Apple related e-books:

Take Control of iCloud, Fifth Edition

Take Control of Parallels Desktop 12

Take Control of OS X Server

Take Control of High Sierra

Take Control of Automating Your Mac

More Articles by © 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.


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's hard to study much history and not dislike religion - (Tony Lawrence)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode