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:
-sReport identical files-cDisplay context mode output-qReport only file differences-yShow side-by-side comparison-rCompare subdirectories recursively-iIgnore case differences-wIgnore whitespace--ignore-file-name-caseIgnore filename case--no-ignore-file-name-caseConsider 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
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
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.
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
-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
Use the diff command followed by two filenames. Run diff file1.txt file2.txt to display differences between files line by line.
Lines starting with < represent the first file, lines with > represent the second file. Numbers indicate line positions and required changes to match files.
Run diff -s file1.txt file2.txt to verify identical files. The command outputs “Files are identical” when content matches exactly.
While diff works with binary files, use cmp command for byte-by-byte comparison. Run cmp file1 file2 for accurate binary file comparison.
Add -w option to ignore all whitespace differences. Run diff -w file1.txt file2.txt to focus only on content changes, not formatting.

