How To Compare two files In Linux

How To Compare two files In Linux






Comparing files is essential for developers and system administrators who track code changes, debug issues, or verify configurations. Linux provides multiple command-line utilities for file comparison, with diff being the most widely used. This guide demonstrates practical methods to compare two files in Linux.

Basic Syntax

The diff command uses this syntax:

diff [OPTION] FILES

Common options include:

  • -s Report identical files
  • -c Display context mode output
  • -q Report only file differences
  • -y Show side-by-side comparison
  • -r Compare subdirectories recursively
  • -i Ignore case differences
  • -w Ignore whitespace
  • --ignore-file-name-case Ignore filename case
  • --no-ignore-file-name-case Consider filename case

Create Files in Linux

Create sample files for comparison testing.

Create the first file:

nano file1.txt

Add this content:

11 12 13 14 15 16 17 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
Numbers in text and numeric format
Sample data file

Create the second file:

nano file2.txt

Add different content:

10 12 3 14 8 16 7 18 19
five twelve eight fourteen fifteen nine seventeen one nineteen
Numbers in text and numeric format
Sample data file

Create a third file identical to the first:

nano file3.txt

Copy the same content from file1.txt:

11 12 13 14 15 16 17 18 19
eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
Numbers in text and numeric format
Sample data file
Note: Use any text editor to create files. The nano editor is beginner-friendly and available on most systems.

Print Differences Between Two Files

Compare file1.txt and file2.txt:

diff file1.txt file2.txt

Output shows only differences:

1,2c1,2
< 11 12 13 14 15 16 17 18 19
< eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
---
> 10 12 3 14 8 16 7 18 19
> five twelve eight fourteen fifteen nine seventeen one nineteen

Display context mode with -c:

diff -c file1.txt file2.txt

Context mode includes surrounding lines:

*** file1.txt	2024-01-08 10:00:00.000000000 +0000
--- file2.txt	2024-01-08 10:00:30.000000000 +0000
***************
*** 1,4 ****
! 11 12 13 14 15 16 17 18 19
! eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
  Numbers in text and numeric format
  Sample data file
--- 1,4 ----
! 10 12 3 14 8 16 7 18 19
! five twelve eight fourteen fifteen nine seventeen one nineteen
  Numbers in text and numeric format
  Sample data file
Tip: Context mode helps understand changes within file structure by showing unchanged lines around modifications.

Find Similar Files Using Diff Command

Check if two files match using -s:

diff -s file1.txt file3.txt

Output confirms identical files:

Files file1.txt and file3.txt are identical

This option verifies file integrity after copying or backup operations.

Find Differ Files Using Diff Command

Use -q to check if files differ:

diff -q file1.txt file2.txt

Output indicates differences:

Files file1.txt and file2.txt differ

This quick check avoids displaying detailed differences when you only need confirmation.

Warning: Binary files require cmp command for accurate comparison. The diff command works best with text files.

Display Output in Side-by-Side View

Compare files side by side with -y:

diff -y file1.txt file2.txt

Side-by-side format shows both files:

11 12 13 14 15 16 17 18 19             |  10 12 3 14 8 16 7 18 19
eveven twelve thirteen fourteen        |  five twelve eight fourteen
Numbers in text and numeric format        Numbers in text and numeric format
Sample data file                          Sample data file

Suppress identical lines using --suppress-common-lines:

diff -y --suppress-common-lines file1.txt file2.txt

Output displays only differences:

11 12 13 14 15 16 17 18 19             |  10 12 3 14 8 16 7 18 19
eveven twelve thirteen fourteen        |  five twelve eight fourteen
Tip: Combine -y with -W to set custom column width for side-by-side comparison.

Conclusion

The diff command offers flexible options for comparing two files in Linux. Use basic comparison for quick checks, context mode for detailed analysis, or side-by-side view for visual comparison. Understanding these options helps automate file verification tasks and streamline development workflows.

FAQs