Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a function to compare 2 binary files, here the requirements:

  • 2 files, not 3 or 4
  • files can't be assumed to exist
  • avoid running checksum (CRC/MD5/SHA/...) until one must
  • if running multiple checksums, do so from least expensive to most expensive (order above)
  • print out meaningful error messages
  • usage: binary_compare_two_files file1 file2

Here's what I have got, I think it can be done much better than this. How?


function binary_compare_two_files() {



  if [ ! "${n_arguments}" -eq $REQUIRED_ARGUMENTS ]; then
    printf 'Invalid number of arguments. Required: %d, supplied: %d\n' \
      $REQUIRED_ARGUMENTS $n_arguments
    echo 'usage: binary_compare_two_files file1 file2'


  if [  ! -f "${file1}" -o ! -f "${file2}" ]; then
    echo 'Invalid arguments. Both arguments need to refer to existing files.'

  file1_size=$(stat -f "%z" "${file1}")
  file2_size=$(stat -f "%z" "${file2}")

  if [ ! ${file1_size} -eq ${file2_size} ]; then
    return $((file1_size - file2_size))

  file1_md5=$(md5 -q "${file1}")
  file2_md5=$(md5 -q "${file2}")

  if [ ! "${file1_md5}" == "${file2_md5}" ]; then
    return -1

  return 0

I have opted not to use diff/bdiff because I am not sure whether those stat and check for sizes first... I would need to look at the src.

share|improve this question

closed as too localized by bahamat, enzotib, manatwork, jasonwryan, Renan Sep 2 '12 at 19:38

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

It is a homework? Otherwise, why not use standard tools, like cmp or diff? – enzotib Sep 2 '12 at 10:12
Not homework, just trying to learn some Bash scripting as I work night shifts at a toll booth. Why not diff? Description read "Compare files line by line." and that sounds inefficient to me. stat first seems instantaneous rather than "line by line". – Robottinosino Sep 2 '12 at 10:23
I don't have money from school but there's plenty of stuff online to learn... plus SO! :) Lemme download the source from diff and see what that does... – Robottinosino Sep 2 '12 at 10:26
@Robottinosino: why are you not using cmp? – Mat Sep 2 '12 at 11:26
diff works for binary files: diff a b gives Binary files a and b differ. cmp may well be better. You definitely don't need a script for this. – terdon Sep 2 '12 at 11:44
up vote 1 down vote accepted
diff -u $1 $2 > /dev/null
if [[ $? -eq 0 ]] ;then
    echo "They are equal!"
    echo "They aren't equal!"

Of course, i didn't test whether argument is free or not.You can do it yourself.

Enjoy that....

share|improve this answer
1) diff has a -q option, don't need redirecting output. 2) you could use if diff instead of using $?, it is more terse. – enzotib Sep 2 '12 at 13:06

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