If you work with text files on Linux or Unix systems, you’ll eventually come across the diff command. The diff command is one of the most useful tools for comparing two files to find what’s changed. In this guide, we'll explain what the diff command does, how to use it to compare two files and find the differences in Linux with examples.
Table of Contents
What is diff Command?
The diff command (short for "difference") is used to compare the contents of two files line by line and display the differences between them. It is a powerful tool for identifying changes, especially in text files like code, configuration files, or logs.
Linux diff Command Usage
The most basic form of the diff command is:
diff [OPTION]… FILES
Example:
diff file1.txt file2.txt
This command compares file1.txt and file2.txt and shows the differences line by line.
Understanding diff Output
When you run diff, it provides output that may look a bit confusing at first. Here’s how to understand it:
- Lines starting with
<indicate content from the first file. - Lines starting with
>indicate content from the second file. - Numbers (like
3c3) show line numbers where changes occurred.
Commonly Used diff Flags
| Flag | Description |
|---|---|
-u | Unified format (used in patches and Git) |
-y | Side-by-side comparison |
-q | Quiet mode – only reports if files differ |
-r | Recursive directory comparison |
--exclude=PATTERN | Skip matching files/directories |
-i | Ignore case differences |
-w | Ignore all whitespace differences |
-b | Ignore changes in the number of spaces |
How to Compare Files using diff Command in Linux
Let's say you have two files, file1.txt and file2.txt, with the following content:
file1.txt:
Line 1: This is the first line. Line 2: This is the second line. Line 3: This is the third line. Line 4: This is the fourth line.
file2.txt:
Line 1: This is the first line. Line 2: This is the second line, but changed. Line 3: This is the third line. Line 4: This is the fourth line, but different.
To compare the contents of these two files using diff, just run:
diff file1.txt file2.txt
This compares file1.txt and file2.txt. If the files are the same, it shows nothing. If they differ, it tells you how to make one file match the other using additions and deletions.
Sample Output:
2c2 < Line 2: This is the second line. --- > Line 2: This is the second line, but changed. 4c4 < Line 4: This is the fourth line. --- > Line 4: This is the fourth line, but different.
Explanation of the Output:
2c2: Indicates that the change is on line 2 in both files (i.efile1.txtandfile2.txt).cindicates changed.< Line 2: This is the second line.: The line fromfile1.txt.---: A separator.> Line 2: This is the second line, but changed.: The line fromfile2.txt.4c4: Indicates that the change is on line 4 in both files.< Line 4: This is the fourth line.: The line fromfile1.txt.---: A separator.> Line 4: This is the fourth line, but different.: The line fromfile2.txt.
Still don't understand? Ok. Let us see another example.
Create two more files namely file3.txt and file4.txt with some random content.
file3.txt:
Hello, world! This is a test. Linux is fun.
file4.txt:
Hello, world! This is a demo. Linux is great.
As you can see in the above files, except the first line, other lines are different. Let us see what diff command will provide in this example.
diff file3.txt file4.txt
Sample Output:
2,3c2,3 < This is a test. < Linux is fun. --- > This is a demo. > Linux is great.
Allow me to explain the above output.
As I already stated, the lines starting with < are from file3.txt and the lines starting with > are from file4.txt.
2,3c2,3 means lines 2-3 in file3.txt were changed (c) to lines 2-3 in file4.txt.
Essential diff Command Examples for Beginners
Here are some essential diff commands and options:
1. Show difference between files
If we use the -u flag with the diff command, it displays the output in the unified format, which presents differences in a more compact and readable way.
diff -u file1.txt file2.txt
Sample Output:
--- file1.txt 2025-05-01 12:47:19.349888344 +0530 +++ file2.txt 2025-05-01 12:47:30.757065330 +0530 @@ -1,4 +1,4 @@ Line 1: This is the first line. -Line 2: This is the second line. +Line 2: This is the second line, but changed. Line 3: This is the third line. -Line 4: This is the fourth line. +Line 4: This is the fourth line, but different.
2. Case-insensitive comparison
The -i flag in the diff command makes the comparison case-insensitive, meaning it will treat uppercase (A-Z) and lowercase (a-z) letters as identical when comparing files.
diff -i file1.txt file2.txt
You can combine -i with other flags like -w (ignore whitespace) for more flexible comparisons.
3. Ignore all whitespace differences
The -w flag ignores ALL whitespace differences when comparing files, including:
- Spaces ( )
- Tabs (
\t) - Line endings (
\r\nvs\n) - Any combination/amount of whitespace
diff -w file1.txt file2.txt
4. Ignore changes in the amount of whitespace
The -b flag ignores differences in the number of whitespace characters (spaces/tabs), but still cares about whitespace presence/position.
diff -b file1.txt file2.txt
5. Side-by-Side comparison
diff -y file1.txt file2.txt
The -y compares two text files, file1.txt and file2.txt, and shows their differences side by side.
Sample Output:
Line 1: This is the first line. Line 1: This is the first line. Line 2: This is the second line. | Line 2: This is the second line, but changed. Line 3: This is the third line. Line 3: This is the third line. Line 4: This is the fourth line. | Line 4: This is the fourth line, but different.
6. Only show if files differ
You can use the -q flag (quite mode) with diff command to only show whether files differ without printing the actual differences.
diff -q file1.txt file2.txt
If the files are identical, it prints:
Files file1.txt and file2.txt are identical
If the files are different, it prints:
Files file1.txt and file2.txt differ
It does not show line-by-line changes - just a yes/no answer.
This is useful when:
- You want to check file integrity or changes in scripts
- You're comparing backups
- You need a fast answer without reading the diff output
7. Recursive directory comparison
The diff command can recursively compare files in two directories using the -r flag.
diff -r dir1/ dir2/
It walks through both directories, compares files with the same names and paths, and reports:
- Which files are different
- Which files exist in one directory but not the other
- Optional: the specific line differences, just like with individual files
Sample Output:
Only in dir2/: file3.txt Only in dir2/: file4.txt
This is very useful when:
- You're checking if two backups are the same
- You're comparing staged vs. deployed content
- You want a quick overview without full diffs
8. Exclude certain files or file types
To exclude certain files or file types while using diff -r to compare directories, you can use the --exclude option.
Syntax:
diff -r --exclude=PATTERN dir1/ dir2/
PATTERNcan be a filename, file extension, or wildcard pattern.- You can use this multiple times to exclude different patterns.
Let us see some examples on how to use the --exclude option with diff command.
1. Exclude all .log files:
diff -r --exclude='*.log' dir1/ dir2/
2. Exclude hidden files (dotfiles):
diff -r --exclude='.*' dir1/ dir2/
3. Exclude node_modules directory:
diff -r --exclude='node_modules' dir1/ dir2/
4. Combine multiple exclusions:
diff -r --exclude='*.log' --exclude='*.tmp' --exclude='backup' dir1/ dir2/
Note:
- The
--excludepattern matches against the basename of the file or directory (not the full path). - It’s case-sensitive unless used with
--ignore-case, which only affects line content, not paths.
9. Comparing Output from Commands
You can use process substitution to compare command outputs:
diff <(ls dir1) <(ls dir2)
This compares the list of files in two directories. It is very handy for scripting or checks.
10. Understanding Exit Codes
diff can be used in scripts. It returns:
0if files are the same1if files differ2if there was an error (e.g. file missing)
You can use this in if statements in Bash.
11. Creating Patches
You can save diff output to a file and apply it later using patch:
diff -u old.txt new.txt > changes.patch patch old.txt < changes.patch
This is useful in software development, backups, and configuration changes.
12. Show Linux diff Command Help
To display help section, use --help.
diff --help
When Not to Use diff Command
For large directories, or when you need checksums or detailed control, rsync, cmp, or md5sum may be better. diff works best for text-based differences, not binary or performance-sensitive tasks.
Conclusion
The diff command is a simple yet powerful tool for comparing files and directories in Linux. Once you understand its output format and key options, you’ll find it useful for troubleshooting, coding, and system administration.
Try experimenting with diff on your own files. Spotting differences will soon be your second nature!
Have any questions or tips about using diff? Please share them in the comments below!


