Introduction
In the day to day of system administrators, developers and advanced Linux users, you often need to know what differences exist between two versions of the same file. Whether to review changes to source code, validate settings or simply confirm that a backup is identical to the original, commanddiffbecomes an indispensable tool. This small but powerful program analyzes two text and sample files, line by line, where they match and where they diverge, offering a clear vision that facilitates decision-making and problem cleansing.
Basic syntax
The simplest way to invokediffis to indicate the two files you want to compare:diff archivo1.txt archivo2.txt. Without additional options, the output uses the normal format, which shows the lines to be changed in the first to get the second, preceded by symbols like<(file lines 1) and>(file 2 lines). This mode is useful for fast comparisons, although it may be less legible when files are extensive or contain many changes.
Most commonly used options
-u(unified): generates a unified format that includes context lines before and after each change, which improves legibility and is the standard in patches.-c(context): similar to the unified but with a more traditional context style, useful to review changes in larger blocks.-i: It ignores capital and lower capital differences, ideal when the case is not relevant.-w: removes all blank spaces when comparing, so that only real differences of content are considered.-b: ignores changes in the amount of blank spaces, treating multiple spaces like one.-r: tour directories recursively, comparing all files with the same name in subtrees.
Practical examples
Suppose we have two versions of a script:script.shandscript_new.sh. To see a unified diff with three context lines, it runs:diff -u script.sh script_new.sh. The output will show something like:
--- script.sh2023-10-01 12: 00: 00.000000000 + 0000 + + + script _ new.sh2023-10-02 09: 15: 00.000000000 + 0000 @ @ @ -5.7 + 5.7 @ echo "Initiating process" - sleep 5 + sleep 10 echo "Waiting entry" read entry
This indicates that on line 6 was changedsleep 5bysleep 10. If you want to ignore blank spaces, add-w: diff -w -u archivo1.cfg archivo2.cfg.
Advanced use
Beyond the basic file-to-file comparison,diffcan work with standard input, allowing it to be integrated into pipes. For example,cmd1 | diff - archivo2.txtcompare the output ofcmd1with a static file. Another useful technique is to combinediffwithpatch: the unified format generated bydiff -uis applied directly withpatch -p0 < cambios.patchto update a source tree. In addition, the option-qonly informs if the files differ, without showing details, which is practical in verification scripts where a fast boolean is needed.
Tips and tricks
- Use
diff -yfor a side-to-side view showing the differences in columns, very useful in wide terminals. - When comparing directories, combine
-rwith--exclude=patternto omit certain types of files, such as--exclude='*.log'. - To save the diff in a legible file, redirect the output:
diff -u orig.txt mod.txt > cambios.diff. - In CI / CD environments, it is common to use
diff -uas a validation step to ensure that no unwanted changes are made to configuration files. - Remember that
diffworks with text; for binary files prefercmpor tools likehexdump.
Conclusion
The commanddiffremains a fundamental part of any Linux user's arsenal thanks to its simplicity, power and flexibility. From a quick comparison of two texts to the generation of complex patches to update large projects, your options allow you to adapt the output to almost any review scenario. Domain your variants and combine it with other tools likepatch, grepor automation scripts gives you precise control over changes in your files, facilitating error detection, configuration audit and collaboration in development equipment.


