A
A
Adept0062018-09-24 16:59:47
linux
Adept006, 2018-09-24 16:59:47

How to compare two files (by md5 or something else) in bash?

You need to compare two files (for example, /tmp/file1 and /tmp/file2) for hash or content similarity so that the result is True or False, respectively

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2018-09-24
@Adept006

For text files, you can try with diff:
If the files are the same, nothing will be displayed. If they differ, it will throw out the differences.

if [ -z "$(diff -q file1  file2)" ]; then
  echo "Одинаковые"
else
  echo "Они такие разные!"
fi

upd. Thanks to nick3iro for the hint about the command cmp: it compares files byte by byte and exits with status 0 if the files are identical; 1 if different; 2 if an error occurred. You can use it like this in a script:
if cmp -s  file1  file2 ; then
  echo "Одинаковые"
else
  echo "Они такие разные!"
fi

V
vanoc, 2018-09-24
@Zoro

a=$(md5sum $file_a | awk '{print $1}')
b=$(md5sum $file_b | awk '{print $1}')
if ; then
echo "no change"
else
echo "files are different"
fi

J
jcmvbkbc, 2018-09-24
@jcmvbkbc

if cmp -s file1 file2 ; then echo Same ; else echo Different ; fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question