How do I compare two files with a shell script?

9

2

Given two files, I want to write a shell script that reads each line from file1 and checks if it is there in file2. If a line is not found it should output two files are different and exit. The files can contain words numbers or anything. For example :

file1 :

Hi!
1234
5678
1111
hello

file2:

1111
5678
1234
Hi!
hello

In this case two files should be equal. if file2 has "hello!!!" instead of "hello" then the files are different. I'm using bash script. How can I do this. It is not important that I need to do it in a nested loop but that's what I thought is the only way. Thanks for your help.

0x0

Posted 2011-01-18T17:27:07.297

Reputation: 267

Answers

9

In bash:

diff --brief <(sort file1) <(sort file2)

Ignacio Vazquez-Abrams

Posted 2011-01-18T17:27:07.297

Reputation: 100 516

What if the file is a csv file. would sorting still work ? – 0x0 – 2011-01-18T17:36:55.037

sort doesn't care about the exact contents unless you tell it to. – Ignacio Vazquez-Abrams – 2011-01-18T17:37:47.827

Is it possible to find which lines differ ? – 0x0 – 2011-01-19T02:07:04.283

Remove --brief and add format options, e.g. -u. – Ignacio Vazquez-Abrams – 2011-01-19T02:07:43.800

10

diff sets its exit status to indicate if the files are the same or not. The exit status is accessible in the special variable $?. You can expand on Ignacio's answer this way:

diff --brief <(sort file1) <(sort file2) >/dev/null
comp_value=$?

if [ $comp_value -eq 1 ]
then
    echo "do something because they're different"
else
    echo "do something because they're identical"
fi

Doug Harris

Posted 2011-01-18T17:27:07.297

Reputation: 23 578

3You can just do if diff ... >/dev/null without the brackets and variable. – Paused until further notice. – 2011-01-18T20:34:05.387

1

Whilst diff is a perfectly fine answer, I'd probably use cmp instead which is specifically for doing a byte by byte comparison of two files.

https://linux.die.net/man/1/cmp

Because of this, it has the added bonus of being able to compare binary files.

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

I'm led to believe it's faster than using diff although I've not personally tested that.

Richard

Posted 2011-01-18T17:27:07.297

Reputation: 4 197

Wouldn't the "files are different" case go first? The if test asks if something is true, ie, a nonzero return code. If the files match, cmp returns 0 (per the manpage), and so that should be the second case. – user8162 – 2019-03-10T19:56:14.963

@user8162 What you say makes sense, however I've just tested it and that is the right way around. I'm not sure why that is the case to be honest. – Richard – 2019-03-12T22:33:57.210

1

Should also work :

comm -3 file1 file2

I think this is enough characters for an answer...

mpez0

Posted 2011-01-18T17:27:07.297

Reputation: 2 578

1

Adding this because I think the [[ ]] && || construct is pretty neat:

#!/bin/bash

[[ `diff ${HOME}/file1 ${HOME}/file2` ]] &&  
   (echo "files different") ||
   (echo "files same")

mmrtnt

Posted 2011-01-18T17:27:07.297

Reputation: 11