Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!mit-eddie!uw-beaver!zephyr.ens.tek.com!tektronix!nosun!tessi!onion!jeff From: jeff@onion.pdx.com (Jeff Beadles) Newsgroups: comp.unix.questions Subject: Re: how to compare file modification time in bourne shell script Message-ID: <1990Jul28.151406.11210@onion.pdx.com> Date: 28 Jul 90 15:14:06 GMT References: <1990Jul23.233044.2729@silma.com> <8855@jpl-devvax.JPL.NASA.GOV> Organization: Little to none Lines: 69 In <8855@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: :In article <1990Jul23.233044.2729@silma.com> aab@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@onion.pdx.com jeff@quark.wv.tek.com