(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



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 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 http://toadstool.se/journal/2006/01/11/the-fallacy-of-ctime 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`
year=$8
thisyear=`date "+%Y"`
case $8 in
   *:*) year=$thisyear;;
esac
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:

#!/usr/bin/perl
$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
yourscript
 

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

($atime,$mtime,$ctime)=(stat($rfile))[8..10];
 

Technorati tags:


Click here to add your comments





Wed Nov 30 23:07:58 2005: Subject:   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: Subject:   TonyLawrence

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

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Auto FTP Manager

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Perl
       - Shell
       - Programming


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress