Bash Script to Compare Two Files with Output
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
How to Think About It
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
Code
#!/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
Dry Run
Let's trace comparing file1.txt with content 'Hello\nWorld' and file2.txt with content 'Hello\nBash'.
Run diff command
diff file1.txt file2.txt
Check diff exit status
Exit status is non-zero because files differ
Print differences
Output: 2c2 < World --- > Bash
| Step | Command | Exit Status | Output |
|---|---|---|---|
| 1 | diff file1.txt file2.txt | 1 | 2c2 < 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
#!/bin/bash file1="$1" file2="$2" if cmp -s "$file1" "$file2"; then echo "Files are identical." else echo "Files differ." fi
#!/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
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| diff command | O(n) | O(1) | Detailed line-by-line differences |
| cmp command | O(n) | O(1) | Quick binary difference check |
| sha256sum hash | O(n) | O(1) | Fast equality check without details |
diff -q file1 file2 for a quick yes/no answer if files differ without details.