Search code examples
linuxfileunixdiff

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


I have a shell script in which I need to check whether two files 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?


Solution

  • I believe cmp will stop at the first byte difference:

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