Fastest way to tell if two field have the same contents in Unix/Linux?

JDS picture JDS · Oct 15, 2012 · Viewed 199.4k times · Source

I have a shell script in which I need to check whether two fields contain the same data or not. I do this a for a lot of files, and in my script the diff command seems to be the performance bottleneck.

Here's the line:

diff -q $dst $new > /dev/null

if ($status) then ...

Could there be a faster way to compare the files, maybe a custom algorithm instead of the default diff?

Answer

Alex Howansky picture Alex Howansky · Oct 15, 2012

I believe cmp will stop at the first byte difference:

cmp --silent $old $new || echo "files are different"