Linux provides a versatile tool known as
diff for comparing files and
directories. It helps users to identify differences between two text files line
by line. Programmers often use
diff to track changes in source code or
The basic syntax of the
diff command is:
diff [OPTION]... FILES
FILESare the two files you want to compare.
[OPTION]...represents the various options that can modify the behavior of
Below is a table of some common options for the
|Output a normal diff (default).
|Report only if files differ.
|Output in two columns.
|Ignore case differences in file contents.
|Ignore all whitespace.
|Recursively compare any subdirectories found.
|Output unified diff format.
|Output diff with context.
|Colorize the output.
|Display a help message and exit.
|Display version information and exit.
Creating Example Files
Let's create two example files to demonstrate the use of the
File 1: original.txt
ito switch to insert mode and enter:
Save and exit with
File 2: modified.txt
Add the following content:
Save and exit with
Example 1: Basic
To compare two files in the normal
diff original.txt modified.txt
diff output uses
< to indicate lines from
c indicates a change between the files.
Example 2: Unified Format
Unified format provides a more compact view of the changes:
diff -u original.txt modified.txt
@@ -1,4 +1,4 @@
The unified format displays a few lines of context by default, showing changes
+ for additions and
- for deletions.
Example 3: Side-by-Side Comparison
To compare files side by side:
diff --side-by-side original.txt modified.txt
Apple | apple
Date | Dragonfruit
The side-by-side view shows both files next to each other, with a
Example 4: Ignoring Case
If you want to compare files but ignore case differences:
diff -i original.txt modified.txt
There will be no output if the only difference is case since
to ignore these differences.
Example 5: Recursive Diff
To compare directories recursively, use
-r. Let's assume
contain our example files:
diff -r dir1 dir2
This will list the differences between any files found in the directories and their subdirectories.
diff with Other Commands
diff can be combined with other commands, such as
less, for more
diff original.txt modified.txt | less
This will pipe the output of
less for easy scrolling through many
diff command is a cornerstone of file comparison in Linux, crucial for
users who need to track changes between file versions. Understanding its options
allows for customized comparisons, fitting the needs of any user
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.