I got a mission to compare the content of hundreds of pair files. I have to quickly compare each pair and to provide an answer if that pair is equal (in content) or not. The files could be text or binary files. Can you help me please?

  1. I'm looking for a command that works on ALL UNIX \ Linux editions
  2. I'd prefer to use the most efficient command (in regards to time and performance)
  3. I'd prefer a command that supports text and binary files
  4. I'd prefer a command that is installed as part of the OS (not a 3rd-party)

Thank you!

  • 2
    I'd recommend diff - man diff – codeling Oct 27 '13 at 7:17

If all you need to know is whether the files differ or not, use cmp. Or, more precisely:

if cmp "$FILE1" "$FILE2"; then
  echo same
else
  echo different
fi

(Or whatever you need to do when they're equal/not-equal). cmp should be present on any posix-like system, works on both binary and text files, and returns immediately when it finds a difference, which is about as fast as you're going to get. (Also, because it's not line-aware, it doesn't waste time finding line-endings.)

diff command should do the job. Use it with -q option to print only the file names which differ in their contents.

diff -q file1 file2

If you want to entire files in a pair of directory, add -r option

diff -r -q dir1 dir2

Refer documentation for more details by typing man diff

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.