Mastering the `diff` Command in Linux
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
configuration files.
Syntax
The basic syntax of the diff
command is:
diff [OPTION]... FILES
FILES
are the two files you want to compare.[OPTION]...
represents the various options that can modify the behavior ofdiff
.
Options
Below is a table of some common options for the diff
command:
Option | Shorthand | Description |
---|---|---|
--normal | Output a normal diff (default). | |
--brief | Report only if files differ. | |
--side-by-side | -y | Output in two columns. |
--ignore-case | -i | Ignore case differences in file contents. |
--ignore-all-space | -w | Ignore all whitespace. |
--recursive | -r | Recursively compare any subdirectories found. |
--unified | -u | Output unified diff format. |
--context | -C | Output diff with context. |
--color | Colorize the output. | |
--help | Display a help message and exit. | |
--version | Display version information and exit. |
Creating Example Files
Let's create two example files to demonstrate the use of the diff
command.
File 1: original.txt
vim original.txt
Press
i
to switch to insert mode and enter:Apple
Banana
Cherry
DateSave and exit with
:wq
.File 2: modified.txt
vim modified.txt
Add the following content:
apple
Banana
Cherry
DragonfruitSave and exit with
:wq
.
Example 1: Basic diff
To compare two files in the normal diff
format:
diff original.txt modified.txt
Output:
1c1
< Apple
---
> apple
4c4
< Date
---
> Dragonfruit
The diff
output uses <
to indicate lines from original.txt
and >
for modified.txt
. The 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
Output:
--- original.txt
+++ modified.txt
@@ -1,4 +1,4 @@
-Apple
+apple
Banana
Cherry
-Date
+Dragonfruit
The unified format displays a few lines of context by default, showing changes
with +
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
Output:
Apple | apple
Banana Banana
Cherry Cherry
Date | Dragonfruit
The side-by-side view shows both files next to each other, with a |
indicating
differing lines.
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 -i
tells diff
to ignore these differences.
Example 5: Recursive Diff
To compare directories recursively, use -r
. Let's assume dir1
and dir2
contain our example files:
diff -r dir1 dir2
Output:
This will list the differences between any files found in the directories and their subdirectories.
Combining diff
with Other Commands
diff
can be combined with other commands, such as grep
or less
, for more
advanced operations:
diff original.txt modified.txt | less
This will pipe the output of diff
to less
for easy scrolling through many
differences.
Conclusion
The 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.