nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

How do i find difference between 2 file dates in shell scriptting

This is a discussion on How do i find difference between 2 file dates in shell scriptting within the Shell scripting forums, part of the Development/Scripting category; HI I need script which can find difference between two dates Example: TS1="Feb 25 20:36:55 IST 2009" TS2="Feb 26 02:36:55 ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 27-02-2009, 01:00 PM
Junior Member
User
 
Join Date: Feb 2009
OS: Debian
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
BharathiNayak is on a distinguished road
Default How do i find difference between 2 file dates in shell scriptting

HI
I need script which can find difference between two dates
Example:
TS1="Feb 25 20:36:55 IST 2009"
TS2="Feb 26 02:36:55 IST 2009"

I should get output as 06:00:00

TS1="Feb 25 20:36:55 IST 2009"
TS2="Feb 26 22:36:55 IST 2009"

Output should be 1Day:02:00:00

Please need help to resolve this

I had tried script but not working properly
#!/bin/bash
TS1="Feb 25 20:36:55 IST 2009"
TS2="Feb 26 02:36:55 IST 2009"
echo TS1 is $TS1 ;
echo TS2 is $TS2 ;
Y1=`echo $TS1 | awk -F " " '{ print $NF }'`
echo Year1 is $Y1 ;
Y2=`echo $TS2 | awk -F " " '{ print $NF }'`
echo Year is $Y2 ;
M1=`echo $TS1 | awk -F " " '{ print $1 }'`
echo "Month1 is $M1" ;
M2=`echo $TS2 | awk -F " " '{ print $1 }'`
echo "Month2 is $M2 ";
Day1=`echo $TS1 | awk -F " " '{ print $2 }'`
echo "Day1 is $Day1 ";
Day2=`echo $TS2 | awk -F " " '{ print $2 }'`
echo "Day2 is $Day2 ";
HR1=`echo $TS1 | awk -F " " '{ print $3 }' | awk -F ":" '{ print $1 }' `
echo HR1 is $HR1
HR2=`echo $TS2 | awk -F " " '{ print $3 }' | awk -F ":" '{ print $1 }' `
echo HR2 is $HR2
M1=`echo $TS1 | awk -F " " '{ print $3 }' | awk -F ":" '{ print $2 }' `
echo Minute1 $M1
M2=`echo $TS2 | awk -F " " '{ print $3 }' | awk -F ":" '{ print $2 }' `
echo Minute1 $M2
S1=`echo $TS1 | awk -F " " '{ print $3 }' | awk -F ":" '{ print $3 }' `
echo Second1 is $S1
S2=`echo $TS2 | awk -F " " '{ print $3 }' | awk -F ":" '{ print $3 }' `
echo Second2 is $S2
if [ $S1 -lt $S2 ]
then
Second_Diff=`expr $S2 - $S1`
else
S2=`expr $S2 + 60`
M2=`expr $M2 - 1`
Second_Diff=`expr $S2 - $S1`
fi
echo Second_Diff is $Second_Diff
#### End of seconds
### Start for Minutes comparison###
if [ $M1 -lt $M2 ]
then
Minute_Diff=`expr $M2 - $M1`
else
M2=`expr $M2 + 60`
Minute_Diff=`expr $M2 - $M1`
HR2=`expr $HR2 - 1`
fi
echo Minute Diff $Minute_Diff
### End of Minutes
### Compare Hour comparision####
if [ $HR1 -lt $HR2 ]
then
Hour_Diff=`expr $HR2 - $HR1 + 24`
else
HR2=`expr $HR2Hr + 24`
Hour_Diff=`expr $HR2 - $HR1`
fi
echo "Second Diff is $Second_Diff"
echo "Minute Diff is $Minute_Diff"
echo "HR_Diff is $Hour_Diff";
printf "Total time taken for build is %2.2d:%2.2d:%2.2d\n" $Hour_Diff $Minute_Diff $Second_Diff

Appreciate your help to resolve this
Thanks
Bharathi
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Find Unix Linux File / Directory by date And Then Copy / Move File asim.mcp CentOS / RHEL / Fedora 1 10-08-2008 03:30 AM
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 31-05-2008 06:44 PM
writing a shell script to find out my shell name jaymob123 Shell scripting 1 08-10-2007 12:36 AM
Difference between ext2 and ext3 Linux file system nixcraft Getting started tutorials 0 18-05-2007 01:43 AM
Difference between Linux Swap partition & Swap file hrishikesh Linux software 9 16-03-2007 02:08 AM


All times are GMT +5.5. The time now is 03:53 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38