# Unix: comparing modification date/time to current time



## Bill Ma (Jun 3, 2009)

How do I compare the current date/time to the modification date/time of a file?


----------



## lotuseclat79 (Sep 12, 2003)

Hi Bill,

To get the modification date/time of a file, issue the command without the brackets <>:
$ date -r <filename>
To get the current date/time, issue the command:
$ date

To compare the two, save each in a variable, and then compare the values of the variables via picking off the delimited fields of the date/time.

To save the current date/time in a variable, issue the command:
$ date=`date`
Then to display the value of the variable date, issue the command:
$ echo "date = $date"
To save the modification date/time of a file, issue the command:
$ modtime=`date -r <filename>`
The to display the value of the variable modtime, issue the command:
$ echo "modtime = $modtime"

To compare the current/time vs modification time of the file, you will need to use one of awk, nawk or gawk - gawk or nawk would be a better choice than awk.

Here is an example:
Let's say that the output of each command above echos the following values:
modtime = Fri Apr 17 17:14:40 EDT 2009
and
date = Wed Jun 3 16:13:16 EDT 2009

As you can see there are 6 fields, and the character ":" is a delimiter in the time field which is also referred to $4 in an awk|nawk|gawk program.

So, if you want to just echo the 4th field in each variable, you would issue the following commands for date and modtime:
$ echo $modtime | nawk '{print $4}'
and
$ echo $date | nawk '{print $4}'

Ok, now that you know how to echo the times to be compared, you now need to create a small awk|nawk|gawk program to use FS=":" as the field separator and save each component of the time, i.e. hh:mm:ss to variable names that you can pick off and compare much like the above commands, and also do the same with the month and time fields of the dates being compared.

Search the Internet for an awk or nawk or gawk tutorial which will show you more about the details of constructing the small awk|nawk|gawk program to do the comparison.

-- Tom


----------



## Bill Ma (Jun 3, 2009)

date -r ---> the -r is not a valid option -- using Solaris


----------

