[NTLUG:Discuss] How to tell file1 is half an hour older than file2?

MadHat madhat at unspecific.com
Mon Apr 29 11:51:57 CDT 2002


stat, sed, awk, grep, echo and test are GNU tools.
He said without gnu tools.  

I mean in perl, it is just a few lines (adding a few for a usage
statement to make it pretty).  Tcl isn't much different, or PHP (which
can be used as a CLI script, but I wouldn't do it).

#!/usr/bin/perl
if ($ARGV[0] && $ARGV[1]) {
  @data1 = stat $ARGV[0];
  @data2 = stat $ARGV[1];
  if ($data1[9] > $data2[9]) { 
    print "$ARGV[0] is newer\n" 
  } else {
    print "$ARGV[1] is newer\n" 
  }
} else {
  print "Usage: $0 <file1> <file2>
  will return the newer file based on mtime (time the file was last
modified)\n\n";
}


I mean if you want to use stat, you can just do
stat -t $1 | awk -F' ' {print'$13'}
to get the date in seconds since the epoc and compare them directly, no
need to convert them.

There are hundreds of ways of doing this, but most use GNU tools, which
he said he does not have (which seems odd to me, but...)

On Mon, 2002-04-29 at 10:32, Paul Ingendorf wrote:
> I know you can do file1 -ol file2 but for time I'm pretty sure you would have to write your own function to tell for sure.
> 
> Just think it out logically it might take a bit of time but it isn't all that hard per se.
> 
> function returnTimeDiff {
> 	echo some code here  for file $1 and file $2
> }
> 
> What I would recommend is turning the dates into a single number via whatever algorithm you like.  I.E. year in seconds/minutes Same for month day and hour then add your minutes or seconds and subtract the two and check the difference.
> 
> The following should show you how easy it is.  The following script requires sed, awk, and stat all GNU tools installed on most systems.
> 
> # ---------------------------------- Example -----------------------------------
> # Author Paul Ingendorf
> function returnYear {
> 	echo $1 | awk -F\| {print'$6'}
> }
> function returnMonthNum {
> 	month=`echo $1 | awk -F\| {print'$3'}`
> 	case "$month" in
> 		Jan)
> 			echo 1
> 			;;
> 		Feb)
> 			echo 2
> 			;;
> 		Mar)
> 			echo 3
> 			;;
> 		Apr)
> 			echo 4
> 			;;
> 		May)
> 			echo 5
> 			;;
> 		Jun)
> 			echo 6
> 			;;
> 		Jul)
> 			echo 7
> 			;;
> 		Aug)
> 			echo 8
> 			;;
> 		Sep)
> 			echo 9
> 			;;
> 		Oct)
> 			echo 10
> 			;;
> 		Nov)
> 			echo 11
> 			;;
> 		Dec)
> 			echo 12
> 			;;
> 		*)
> 			echo 0
> 	esac
> }
> function returnDay {
> 	echo $1 | awk -F\| {print'$4'}
> }
> function returnHour {
> 	echo $1 | awk -F\| {print'$5'} | awk -F: {print'$1'}
> }
> function returnMinute {
> 	echo $1 | awk -F\| {print'$5'} | awk -F: {print'$2'}
> }
> function returnSecond {
> 	echo $1 | awk -F\| {print'$5'} | awk -F: {print'$3'}
> }
> function returnTime {
> 	let Time=`returnMonthNum $1`*24*60*60+`returnDay $1`*24*60*60+`returnHour $1`*60*60+`returnMinute $1`*60+`returnSecond $1`
> 	echo $Time
> }
> let x=1
> for modified in `stat $1 $2  | grep Modify | sed -e "s/ /|/g"`
> 	do
> 		export fileDate$x=$modified
> 		let x=$x+1
> 	done
> let fileTime1=`returnTime $fileDate1`
> let fileTime2=`returnTime $fileDate2`
> let timeDiff=$fileTime2-$fileTime1
> if test `returnYear $fileDate1` -gt `returnYear $fileDate2`
> 	then echo older than 30
> 	else
> 		if test $timeDiff -gt 29
> 			then echo older than 30
> 			else echo not older than 30
> 			fi
> 	fi
> 
> # -------------------------------- End Example ----------------------------------
> 
> -- 
> -->> mailto:pauldy at wantek.net
> -->> http://www.wantek.net/
> Running ....... Cos anything else would be a waste...
> `:::'                  .......  ......
>  :::  *                  `::.    ::'
>  ::: .::  .:.::.  .:: .::  `::. :'
>  :::  ::   ::  ::  ::  ::    :::.
>  ::: .::. .::  ::.  `::::. .:'  ::.
> .:::.....................::'   .::::..
> 
> 
> -----Original Message-----
> From: discuss-admin at ntlug.org [mailto:discuss-admin at ntlug.org]On Behalf
> Of Sameer Khan
> Sent: Saturday, April 27, 2002 1:51 PM
> To: discuss at ntlug.org
> Subject: [NTLUG:Discuss] How to tell file1 is half an hour older than
> file2?
> 
> 
> Hello,
> Any ideas how one can go about doing above in
> a ksh script (without GNU tools unfortunately)?
> Thanks for your brain droppings!
> Sameer
> 
> 
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
> 
> 
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
> 
-- 
MadHat at Unspecific.com
gpg --keyserver wwwkeys.us.pgp.net --recv-keys 9DDC3E98
Key fingerprint = E786 7B30 7534 DCC2 94D5  91DE E922 0B21 9DDC 3E98





More information about the Discuss mailing list