SUMMARY: comparing file dates.

From: Oscar Goldes (
Date: Fri Jul 25 1997 - 13:14:55 CDT


Is there a (simple) script or command that allows one to compare the last
modification date of two files? Something that allows you to build a script
like this:

if <file1> <is newer than> <file2>
then ....

Files may reside in different directories. I do not want to use make
because I would need to build the makefile "on the fly". it seems there has
to be a simpler way.
Filtering the output of ls -l is not simple because the format differs if
files are older or newer than 6 months.
Using Solaris 2.5


a) use korn shell and the test function:
if [ file1 -nt file2 ]
        echo first file younger
        echo first file at least as old as second

This can be found in the ksh man page. However, it is NOT in the test/if
man page.... This is exactly what I wanted

b) use the find command
if [ `find file1 -newer file2` ]

c) other suggestions:
c1. use ls -lt file1 file2 to get a time-ordered listing and filter the
first line
c2. use a perl script
c3. use a custom C program. Two members of the list were kind enough to
send me C source files that implement a command to do this, both based in
the fstat system call

I tested both a) and b), any of them will do the job, a) is simpler (in ksh)

I got 18 answers in 3 hours!!

Thanks to
Michael Shon
Steve Boyko
Robert Hill
Trey Valenta
Claude Charest
Niall Broin
Michael Sullivan
Casper Dik
Viet Hoang
Peter Asboeck
Harvey Wamboldt
Tom Vayda
David Brierle
Brion Leary
Shriman Gurung
Brian Styles
Jeff Zabeck
Ray Trzaska

and others.

This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:59 CDT