⚖️ Command: diff (Difference)
The diff command compares two files line-by-line and tells you exactly what has changed between them. It is the fundamental technology behind version control systems like Git.
1. The "Why"
For an Linux user managing projects, diff is your forensic tool.
- Version Control: Compare a backup of your code with your current version to see which change introduced a bug.
- Config Auditing: Before applying a new Hyprland or Waybar configuration, compare it against your working one.
- Draft Tracking: See exactly which paragraphs you edited in your "E-commerce Blueprint" since yesterday.
- Patching: Generate "patch" files that you can send to other developers to apply your changes to their code.
2. Basic Syntax
diff [OPTIONS] file1 file2
- file1: The "original" or older version.
- file2: The "new" or modified version.
3. Understanding the Output
The default output can be a bit cryptic. It uses symbols like:
<: Lines found infile1but missing fromfile2.>: Lines found infile2but missing fromfile1.a/d/c: Added / Deleted / Changed.
4. Practical Examples for Your Workflow
A. The "Unified" View (-u)
This is the most popular format (and what Git uses). It shows context around the changes, using + for additions and - for deletions.
diff -u old_script.java new_script.java
B. Side-by-Side View (-y)
If you have a wide monitor (like on your Ryzen 5 setup), this is often easier to read:
diff -y config_backup.conf config_current.conf
Add --suppress-common-lines to only see what's different.
C. Comparing Entire Directories (-r)
Check differences between two versions of your entire Preader project:
diff -rq ./project_v1 ./project_v2
-r: Recursive (checks subfolders).-q: Brief (only tells you which files differ, not every line).
D. Ignoring Whitespace (-w)
If you only care about code changes and not how you indented your Java or LaTeX code:
diff -w file1.tex file2.tex
5. Creating and Applying Patches
You can save the output of a diff to a file and "apply" it to an original file later using the patch command.
- Create the patch:
diff -u original.java modified.java > fixes.patch - Apply the patch:
patch original.java < fixes.patch
6. Pro-Tips
- Visual Diff: While
diffis great for the terminal, on Arch you might want to trymeldorkomparefor a GUI experience. - Colorized Output: Use the
--colorflag to make deletions red and additions green:diff --color -u file1 file2 - Arch Linux Context: When you update your system with
pacman, you sometimes get.pacnewfiles. These are new configuration files that didn't overwrite your old ones. Usediffto see if there are new features or security settings you should merge into your config.
7. Summary Reference
| Goal | Command |
|---|---|
| Standard Diff | diff file1 file2 |
| Unified (Git-style) | diff -u file1 file2 |
| Side-by-Side | diff -y file1 file2 |
| Compare Directories | diff -r dir1 dir2 |
| Ignore Case/Space | diff -iw file1 file2 |