how to compare file modification time in bourne shell script

Mark Lanzo lanzo at wgate.UUCP
Thu Jul 26 23:03:13 AEST 1990


Someone asks:
>
> 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
> 

Try this:

#!/bin/sh

newer()
    {
    f1="$1"
    set -- `ls -ct -- "$f1" "$2"`
    test "$1" = "$f1"
    return $?			# This line isn't really necessary
    }

as written, this function presupposes that $1 and $2 refer to accessible
files (else the ls command will pop up with a "not found" type of error).



-- 
Mark Lanzo                      Wandel & Goltermann Technologies, Inc.
Home (919)481-2406              1030 Swabia Court
Play (919)941-5730              Research Triangle Park
uunet.uu.net!wgate.com!lanzo    North Carolina 27709-3585



More information about the Comp.unix.questions mailing list