16

I was wondering if anyone could tell me if there is a function available in unix, bash that compares all of the lines of the files. If they are different it should output true/false, or -1,0,1. I know these cmp functions exist in other languages. I have been looking around the man pages but have been unsuccessful. If it is not available, could someone help me come up with an alternative solution?

Thanks

Masterminder
  • 1,127
  • 7
  • 21
  • 31
  • `diff -u file1 file2` Note: diff's output can (and will) be used by the `patch` program to "edit" file1 into file2 (or vice versa) – wildplasser Oct 04 '12 at 21:35

5 Answers5

38

There are several ways to do this:

  • cmp -s file1 file2: Look at the value of $?. Zero if both files match or non-zero otherwise.
  • diff file1 file2 > /dev/null: Some forms of the diff command can take a parameter that tells it not to output anything. However, most don't. After all, you use diff to see the differences between two files. Again, the exit code (you can check the value of $? will be 0 if the files match and non-zero otherwise.

You can use these command in a shell if statement:

if cmp -s file1 file2
then
   echo "The files match"
else
   echo "The files are different"
fi

The diff command is made specifically for text files. The cmp command should work with all binary files too.

Mau
  • 15
  • 6
David W.
  • 105,218
  • 39
  • 216
  • 337
  • What if you want to make the if shorter, and only execute the `if` if files are different? Can I just use `!`? I'm not clear I literally mean to reverse your logic, the first `then` would be "files doesn't match" and else "files match" – Freedo Apr 04 '19 at 10:13
7

There is a simple cmp file file command that does just that. It returns 0 if they are equal and 1 if they are different, so it's trivial to use in ifs:

if cmp file1 file1; then
    ...
fi

Hope this helps =)

  • 2
    just beat me to it. :-) Adding a `-s` option will ensure that it is silent for differing files and only provides the return code so you don't get additional output – TaninDirect Oct 04 '12 at 21:34
2
#!/bin/bash

file1=old.txt
file2=new.txt

echo " TEST 1 : "
echo

if [ $( cmp -s ${file1} ${file2}) ]
then
   echo "The files match :  ${file1} - ${file2}"
else
   echo "The files are different :  ${file1} - ${file2}"
fi

echo
echo " TEST 2 : "
echo
bool=$(cmp -s "$file1" "$file2" )
if cmp -s "$file1" "$file2"
then
   echo "The files match"
else
   echo "The files are different"
fi

echo
echo " TEST 3 : md5 / md5sum - compute and check MD5 message digest"
echo

md1=$(md5 ${file1});
md2=$(md5 ${file2});

mdd1=$(echo $md1 | awk '{print $4}' ) 
mdd2=$(echo $md2 | awk '{print $4}' ) 

# or md5sum depends on your linux flavour :D
#md1=$(md5sum ${file1});
#md2=$(md5sum ${file2});

#mdd1=$(echo $md1 | awk '{print $1}' ) 
#mdd2=$(echo $md2 | awk '{print $1}' ) 

echo $md1
echo $mdd1
echo $md2
echo $mdd2
echo

#if [ $mdd1 = $mdd2 ]; 
if [ $mdd1 -eq $mdd2 ]; 
then
   echo "The files match :  ${file1} - ${file2}"
else
   echo "The files are different :  ${file1} - ${file2}"
fi
kris
  • 392
  • 4
  • 16
0

You could do an md5 on the two files, then compare the results in bash.

No Unix box here to test, but this should be right.

#!/bin/bash

md1=$(md5 file1);
md2=$(md5 file2);

if [ $md1 -eq $ $md2 ]; then
  echo The same
else
  echo Different
fi
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • This will not work because it is always different, because the name of the file is also printed. See in one of the previous answers : TEST 3 : `md1=$(md5 ${file1}); md2=$(md5 ${file2}); mdd1=$(echo $md1 | awk '{print $4}' ) mdd2=$(echo $md2 | awk '{print $4}' ) if [ $mdd1 -eq $mdd2 ]; ` [https://stackoverflow.com/questions/12736013/comparison-function-that-compares-two-text-files-in-unix/36638506#36638506 ] – kris Jan 04 '19 at 12:45
0

echo "read first file" read f1 echo "read second file" read f2

diff -s f1 f2 # prints if both files are identical