[comp.unix.questions] Re: how to compare file modification time in bourne shell script

Chet Ramey chet at cwns1.CWRU.EDU
Thu Jul 26 10:16:48 AEST 1990


Archive-name: newer/25-Jul-90
Original-posting-by: chet at cwns1.CWRU.EDU (Chet Ramey)
Original-subject: Re: how to compare file modification time in bourne shell script
Reposted-by: emv at math.lsa.umich.edu (Edward Vielmetti)

[Reposted from comp.unix.questions.
Comments on this service to emv at math.lsa.umich.edu (Edward Vielmetti).]

In article <1990Jul23.233044.2729 at silma.com> aab at silma.UUCP () writes:

>newer file1 file2
>that returns 0 if file1 is newer than file2 else returns 1

Here's something I picked up a while back...

/*
 * From Henry Spencer
 *
 * > There doesn't appear to be any decent way to compare the last modified
 * > times of files from the shell...
 *
 * Before everybody starts inventing their own names for this, it should be
 * noted that V8 already has a program for this, newer(1).  It takes two
 * filenames as arguments, and exits with status 0 if and only if either
 * (a) the first exists and the second does not, or (b) both exist and the
 * first's modification time is at least as recent as the second's.  Other-
 * wise it exits with non-zero status.  (The preceding two sentences are
 * essentially the whole of the manual page for it.)
 * 
 * Relatively few people have V8, but in the absence of any other precedent
 * for what this facility should like look, it seems reasonable to follow
 * V8's lead.
 * 
 * Here is an independent rewrite, done from the manual page and not the
 * code, by me, hereby placed in the public domain:
 */

/*
 * newer - is first file newer than second?
 *
 * newer file1 file2
 *
 * exit with 0 status if file1 exists and file2 does not, or if file1's last
 * modified time is at least as recent as file2's.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

main(argc, argv)
int argc;
char *argv[];
{
	struct stat file1;
	struct stat file2;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s file1 file2\n", argv[0]);
		exit(2);
	}

	if (stat(argv[1], &file1) < 0)
		exit(1);
	if (stat(argv[2], &file2) < 0)
		exit(0);
	if (file1.st_mtime >= file2.st_mtime)
		exit(0);
	exit(1);
}
-- 
Chet Ramey					``See Figure 1.''
Network Services Group
Case Western Reserve University	
chet at ins.CWRU.Edu		



More information about the Alt.sources mailing list