Introduction
In the day-to-day work of system administrators, developers, and advanced Linux users, it is often necessary to know what differences exist between two versions of the same file. Whether to review changes in source code, validate configurations, or simply confirm that a backup copy is identical to the original, the diff command becomes an indispensable tool. This small but powerful program analyzes two text files and shows, line by line, where they match and where they diverge, offering a clear view that facilitates decision-making and problem debugging.
Basic Syntax
The simplest way to invoke diff is to specify the two files you want to compare: diff file1.txt file2.txt. Without additional options, the output uses the normal format, which shows the lines that must be changed in the first file to obtain the second, preceded by symbols such as < (lines from file 1) and > (lines from file 2). This mode is useful for quick comparisons, although it can become less legible when the files are large or contain many changes.
Most Used Options
-u(unified): generates a unified format that includes context lines before and after each change, improving readability and is the standard in patches.-c(context): similar to unified but with a more traditional context style, useful for reviewing changes in larger blocks.-i: ignores case differences, ideal when case is not relevant.-w: removes all whitespace when comparing, so only real content differences are considered.-b: ignores changes in the amount of whitespace, treating multiple spaces as one.-r: recursively traverses directories, comparing all files with the same name in sub‑trees.
Practical Examples
Suppose we have two versions of a script: script.sh and script_new.sh. To view a unified diff with three lines of context, run: diff -u script.sh script_new.sh. The output will look something like:
--- script.sh 2023-10-01 12:00:00.000000000 +0000 +++ script_new.sh 2023-10-02 09:15:00.000000000 +0000 @@ -5,7 +5,7 @@ echo "Starting process" - sleep 5 + sleep 10 echo "Waiting for input" read entrada
This indicates that on line 6, sleep 5 was changed to sleep 10. If you wish to ignore whitespace, add -w: diff -w -u file1.cfg file2.cfg.
Advanced Usage
Beyond the basic file‑to‑file comparison, diff can work with standard input, allowing it to be integrated into pipelines. For example, cmd1 | diff - file2.txt compares the output of cmd1 with a static file. Another useful technique is to combine diff with patch: the unified format generated by diff -u can be applied directly with patch -p0 < changes.patch to update a source tree. Additionally, the -q option only reports whether the files differ, without showing the details, which is handy in verification scripts where a quick boolean is needed.
Tips and Tricks
- Use
diff -yfor a side‑by‑side view that shows differences in columns, very useful in wide terminals. - When comparing directories, combine
-rwith--exclude=patternto skip certain file types, such as--exclude='*.log'. - To save the diff to a readable file, redirect the output:
diff -u orig.txt mod.txt > changes.diff. - In CI/CD environments, it is common to use
diff -uas a validation step to ensure no unwanted changes are introduced in configuration files. - Remember that
diffworks with text; for binary files prefercmpor
This post is also available in ESPAÑOL.