how to compare file modification time in bourne shell script

Jeff Beadles jeff at onion.pdx.com
Sun Jul 29 01:14:06 AEST 1990


In <8855 at jpl-devvax.JPL.NASA.GOV> lwall at jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
:In article <1990Jul23.233044.2729 at silma.com> aab at silma.UUCP () writes:
:> 
:> I need to compare the modification times of two files in a bourne shell
:> script. I would like to do this without writing C code.
:> Machine is Sparcstation 1 running SunOS 4.03c
:> 
:> Thus I need a function:
:> 
:> newer file1 file2
:> 
:> that returns 0 if file1 is newer than file2 else returns 1
:> 
:> Can it be done?
:
:Several ways.
...
:ls -lt file1 file2 | tail -1 | grep file2 >/dev/null

This will only work some of the time.  What if file1 is "foobar" and file2 is
"bar" If you *had* to use this, I would recomend:

ls -lt file1 file2 | tail -1 | grep "^file2$" >/dev/null


UTek, the OS that I'm using now (UTek, a 4.2based OS.)  It has a couple of 
options to test:



TEST(1)                 COMMAND REFERENCE                 TEST(1)



NAME
     test - condition command

SYNOPSIS
     test expr [ expr ]

DESCRIPTION

...


     -C filename
               Time of the last status change to filename (see
               stat(2))

     -M filename
               Time of the last modification to filename (see
               stat(2))

     -A filename
               Time of the last access to filename (see stat(2))

...

Thus, you can use:
	if test -M file1 -le -M file2 ; then
		...
	else
		...
	fi


	-Jeff
-- 
Jeff Beadles		jeff at onion.pdx.com	jeff at quark.wv.tek.com



More information about the Comp.unix.questions mailing list