How to compare two file contents which created within one hour?

Hi Gurus,

I need to compare two file contents and log the difference. the detail as below:
in current directory there is file

 
abc20140728_1020.txt
abc20140728_1110.txt

I need to find the files which created within 60 minutes first then compare the contents.
I am able to use find command find the files but I don't know how to compare these two file contents.

 
for filename in `find srch_dir -cmin -60 -name abc2014*`
do
here i don't know how to store the first file then use diff $filename1 $filename2
done

Please help me on this issue.

thanks in advance

I must admit that I don't know either. Perhaps an alternate logic might help though:-

  • List all the files that match your criteria and write them to a file (listfile)
  • Get top line of listfile and set that as your main file (file1)
  • Get all other lines of your listfile and in a loop, compare them to file1

Would that be suitable?

In any case, if you have files a , b , c , d & e though, how will you decide which is the main file to be compared to, i.e. file1

Robin

Assuming that any file is suitable as the file to be compared against all of the others, try something like:

find srch_dir -cmin -60 -name 'abc2014*' | (
	read file1
	while read file2
	do	diff "$file1" "$file2"
	done
)
1 Like

thanks Don Cragun

my unix is solaris. when I run

 
find srch_dir -cmin -60 -name 

I got error.
any idea how to convert this to Solaris command.

thanks in advance.

Dear ken6503,

It would be helpful if you posted the error.

Robin

As long as you don't run it any day before 1am, you could try something like:

#!/bin/ksh
IAm=${0##*/}
read x y z <<-EOF
	$(date +'%m%d %H %M%n')
EOF
trap 'rm -f "$IAm.$$"' EXIT
touch -t $(printf '%s%02d%s' $x $((${y#0} - 1)) $z) "$IAm.$$"
find srch_dir -newer "$IAm.$$" -name 'abc2014*' | (
	read file1
	while read file2
	do	diff "$file1" "$file2"
	done
)
1 Like

Hi Robin

the error as below:

 
 
find: bad option -cmin
find: [-H | -L] path-list predicate-list

thanks in advance

If you want to compare every file to every other file just once, try (given you get rid of your find -cmin error):

find * -cmin -60 | while read FN1
   do find * -cmin -60 | tac | while read FN2
      do [ "$FN1" = "$FN2" ] && break
          diff  "$FN1"  "$FN2"
      done
   done
1 Like

Hi Don Cragun
for my script, after finding the files, I need to output to tmp file then do something, then compare.
for below code, how can I put them in while loop.

 
find srch_dir -newer "$IAm.$$" -name 'abc2014*' | (
read file1
while read file2
do diff "$file1" "$file2"
done
)

thanks in advance

Can you be any more vague???
Try:

#!/bin/ksh
IAm=${0##*/}
read x y z <<-EOF
	$(date +'%m%d %H %M%n')
EOF
trap 'rm -f "$IAm.$$"' EXIT
touch -t $(printf '%s%02d%s' $x $((${y#0} - 1)) $z) "$IAm.$$"
find srch_dir -newer "$IAm.$$" -name 'abc2014*' | (
	read file1
	while read file2
	do	echo 'who knows what' > tmpfile	# output to temp file
		# do something else here
		diff "$file1" "$file2"	# compare
	done
)
1 Like

So where does this bit come in if the find doesn't work?

1 Like

Thanks for your reply.

sorry for the vague???

when first time running the script there is no any files in the directory. the find command only find one file, I don't need to compare. what i want to do is after finding the file(s), first I want to count how many files I found, if it is one, I don't compare; if it is two files, I will compare.

hopefully, this time is a little bit clear.

thanks in advance.

Why do you want to count the files. The script I gave you won't do anything unless there are at least two files. If there are two or more files, it will execute the commands in the while loop once for each file except the 1st file. The name of the 1st file is preserved in the variable file1 and doesn't change while the name of each remaining file is stored in the variable file2 as it is processed in the loop.

You have said you want to create a temp file, but you haven't shown us what would go into that temp for nor what it would be used for. Then you said you want to do something else, but you haven't said what. Then, if there are two or more files, you said you want to compare the files; the script I gave you does that.

Did you try running the script I gave you when 0, 1, 2, and 3 files with names starting with abc2014 were present that had been created within the last hour? For each case, what did the script do wrong?