0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Compare Two Files with Output

Use the Bash command diff file1 file2 to compare two files line by line, or write a script using diff and check its exit status to report if files differ.
📋

Examples

Inputfile1.txt content: Hello World file2.txt content: Hello World
OutputNo output (files are identical, diff returns nothing)
Inputfile1.txt content: Hello World file2.txt content: Hello Bash
Output2c2 < World --- > Bash
Inputfile1.txt content: file2.txt content: Hello
Output0a1 > Hello
🧠

How to Think About It

To compare two files in Bash, you can use the diff command which checks differences line by line. The script runs diff on both files and then checks if the output is empty or not to decide if files are the same or different.
📐

Algorithm

1
Get the names of the two files to compare.
2
Run the <code>diff</code> command on the two files.
3
Check the exit status of <code>diff</code>: if zero, files are identical.
4
If exit status is non-zero, print the differences.
5
End the script.
💻

Code

bash
#!/bin/bash

file1="$1"
file2="$2"

if diff "$file1" "$file2" > /dev/null; then
  echo "Files are identical."
else
  echo "Files differ:" 
  diff "$file1" "$file2"
fi
Output
Files are identical. # or if files differ: Files differ: 2c2 < World --- > Bash
🔍

Dry Run

Let's trace comparing file1.txt with content 'Hello\nWorld' and file2.txt with content 'Hello\nBash'.

1

Run diff command

diff file1.txt file2.txt

2

Check diff exit status

Exit status is non-zero because files differ

3

Print differences

Output: 2c2 < World --- > Bash

StepCommandExit StatusOutput
1diff file1.txt file2.txt12c2 < World --- > Bash
💡

Why This Works

Step 1: Using diff command

The diff command compares two files line by line and outputs the differences.

Step 2: Checking exit status

If diff returns exit status 0, files are identical; non-zero means differences exist.

Step 3: Output differences

When files differ, printing diff output shows exactly what lines changed.

🔄

Alternative Approaches

Using cmp command
bash
#!/bin/bash
file1="$1"
file2="$2"

if cmp -s "$file1" "$file2"; then
  echo "Files are identical."
else
  echo "Files differ."
fi
The <code>cmp</code> command checks if files differ but does not show detailed differences, so it's faster but less informative.
Using sha256sum hash comparison
bash
#!/bin/bash
file1="$1"
file2="$2"

hash1=$(sha256sum "$file1" | cut -d' ' -f1)
hash2=$(sha256sum "$file2" | cut -d' ' -f1)

if [ "$hash1" = "$hash2" ]; then
  echo "Files are identical."
else
  echo "Files differ."
fi
Comparing hashes is efficient for large files but does not show where files differ.

Complexity: O(n) time, O(1) space

Time Complexity

The diff command reads both files line by line, so time grows linearly with file size.

Space Complexity

The script uses constant extra space, as diff streams files without loading all content into memory.

Which Approach is Fastest?

Using cmp or hash comparison is faster for large files if you only need to know if files differ, but diff gives detailed differences.

ApproachTimeSpaceBest For
diff commandO(n)O(1)Detailed line-by-line differences
cmp commandO(n)O(1)Quick binary difference check
sha256sum hashO(n)O(1)Fast equality check without details
💡
Use diff -q file1 file2 for a quick yes/no answer if files differ without details.
⚠️
Forgetting to quote file names can cause errors if file names have spaces or special characters.