|
Home > Archive > Unix Shell > November 2005 > File timestamp comparison
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
File timestamp comparison
|
|
| Dale DeRemer 2005-11-16, 7:54 am |
| I've kind of been thrashing on this one, so I figured it's time to ask for
help. I want to write a script to compare 2 file timestamps, and then do
date arithmetic and give a time difference. I do an ls -al on the 2 files,
and pipe the result to a file. I can use awk to read the 7th and 8th fields,
but I want to keep the values. Awk wants to read both lines.
So, how do I store the fields in variables? (e.g. date1, date2, time1,
time2)
Once I have time1 and time2, the format is hh:mm. Any suggestions how I can
do arithmetic on 2 such values?
Thanks.
| |
| Ed Morton 2005-11-16, 7:54 am |
| Dale DeRemer wrote:
> I've kind of been thrashing on this one, so I figured it's time to ask for
> help. I want to write a script to compare 2 file timestamps, and then do
> date arithmetic and give a time difference. I do an ls -al on the 2 files,
> and pipe the result to a file. I can use awk to read the 7th and 8th fields,
> but I want to keep the values. Awk wants to read both lines.
> So, how do I store the fields in variables? (e.g. date1, date2, time1,
> time2)
> Once I have time1 and time2, the format is hh:mm. Any suggestions how I can
> do arithmetic on 2 such values?
> Thanks.
>
>
"ls -al" isn't going to work for you in general. For example:
$ ls -al .bashrc file1
-rwxr-xr-x 1 morton None 747 Apr 25 2005 .bashrc
-rw-r--r-- 1 morton None 18 Oct 23 07:06 file1
Note the different timestamp formats based on how old the file is. If
you want a consistent format for comparison, you can do either of these
with GNU find:
$ find . -maxdepth 1 \( -name .bashrc -o -name file1 \) -printf
"%TY%Tm%Td%TH%TM%TS %f\n"
20050425095223 .bashrc
20051023070659 file1
$ find . -maxdepth 1 \( -name .bashrc -o -name file1 \) -printf "%T@ %f\n"
1114440743 .bashrc
1130069219 file1
where the first gives the modification time in YYYYMMDDHHMMSS format and
the second gives it in seconds since Jan. 1, 1970, 00:00 GMT.
For general data arithmetic, see question 6 in the FAQ
(http://home.comcast.net/~j.p.h/cus-faq.html#6).
Regards,
Ed.
| |
| Chris F.A. Johnson 2005-11-16, 6:03 pm |
| On 2005-11-15, Dale DeRemer wrote:
> I've kind of been thrashing on this one, so I figured it's time to ask for
> help. I want to write a script to compare 2 file timestamps, and then do
> date arithmetic and give a time difference. I do an ls -al on the 2 files,
> and pipe the result to a file. I can use awk to read the 7th and 8th fields,
> but I want to keep the values. Awk wants to read both lines.
> So, how do I store the fields in variables? (e.g. date1, date2, time1,
> time2)
eval "$( ls -l FILE1 FILE2 |
awk '{ printf "month%d=%s date%d=%s time%d=%s ", NR, $6, NR, $7, NR, $8}' )"
> Once I have time1 and time2, the format is hh:mm. Any suggestions how I can
> do arithmetic on 2 such values?
Convert the time to minutes:
m1=$(( ${time1%:*} * 60 + ${time1#*:} ))
m2=$(( ${time2%:*} * 60 + ${time1#*:} ))
diff=$(( $m2 - $m1 ))
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
|
|
|
|
|