how to compare file modification time in bourne shell script

Chris Davies chris at vision.UUCP
Fri Jul 27 02:06:00 AEST 1990


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.

If you insist...  :-)

The function returns 0 if file1 is newer than file2.  Otherwise it returns 1.
Note that there is no check to ensure both files exist.
Syntax: newer file1 file2

newer() {
	F1=$1
	F2=$2

	echo "$F1\n$F2" > /tmp/newer$$
	if ls -t "$F1" "$F2" | cmp -s - /tmp/newer$$
	then
		RS=0
	else
		RS=1
	fi

	rm -f /tmp/newer$$
	return $RS
}

Have fun...
		Chris
-- 
VISIONWARE LTD         | UK: chris at vision.uucp     JANET: chris%vision.uucp at ukc
57 Cardigan Lane       | US: chris at vware.mn.org    OTHER: chris at vision.co.uk
LEEDS LS4 2LE          | BANGNET:  ...{backbone}!ukc!vision!chris
England                | VOICE:   +44 532 788858   FAX:   +44 532 304676
-------------- "VisionWare:   The home of DOS/UNIX/X integration" --------------



More information about the Comp.unix.questions mailing list