Operating System - HP-UX
1830892 Members
2921 Online
110017 Solutions
New Discussion

finding time differences between 2 files

 
Mike Blansfield
New Member

finding time differences between 2 files

Good day,
I need a script that will tell me the time difference between 2 files. I know that has to been been done before but I am having trouble finding it on the net. Acually I found one but it bombs. Any help much appreciated and reciprocated!

Thanks,

Mike Blansfield

3 REPLIES 3
Martin Robinson
Frequent Advisor

Re: finding time differences between 2 files

Shells (csh & sh) have poor time support. I would use the system call 'stat' to get the time info. You can either write a C program or access stat from perl. This will allow you to calculate the time difference in seconds, if that is what you are after.
Martin Robinson
Frequent Advisor

Re: finding time differences between 2 files

Although my perl is not good, this works:

$file1=@ARGV[0];
$file2=@ARGV[1];
@stat1 = stat($file1);
@stat2 = stat($file2);
$time1 = @stat1[9];
$time2 = @stat2[9];
print "File $file1 $time1\n";
print "File $file2 $time2\n";
$diff = $time2 - $time1;
print "Difference is $diff seconds\n";
A. Clay Stephenson
Acclaimed Contributor

Re: finding time differences between 2 files

Here's a Perl script that can optionally determine if a file is older than an arbitrary number of seconds OR (with the -e arg will output the datestamp (in seconds since 1-Jan-1970 0000 UTC). It will by default use modification time but can optionally output last access time or last change (chmod) time.

Invoke as fileage.pl for full usage.

Here is an example of your task:
#!/usr/bin/sh
PROG=${0}
if [[ ${#} -ne 2 ]]
then
echo "Usage: ${PROG} file1 file2" >&2
exit 255
fi
F1=${1}
F2=${2}
shift 2
AGE=$(( $(fileage.pl -e ${F1}) - $(fileage.pl -e ${F2}) ))
echo "${AGE}"
exit 0

If it ain't broke, I can fix that.