That's an excellent follow-up question! When diff -r compares directories, it does two main things:
- Identifies files/directories unique to one side: This is what we saw with the "Only in..." output for the
.desktopfiles. - Compares the content of identical files (by name and path) found in both directories: This is where it gets interesting!
If diff -r finds a file with the same name and relative path in both ~/Desktop and ~/Code (for example, if both directories happened to have a notes.txt file), it will then perform a standard file content comparison on those two specific files.
The output for these content differences will be just like the output you'd get if you ran diff on those two files directly.
Let's illustrate with an example to make it clear. Imagine you have two directories:
dir_A/
├── file1.txt
└── subdir/
└── file2.txt
dir_B/
├── file1.txt
└── subdir/
└── file2.txt
└── unique_file_B.txt
And their contents are:
dir_A/file1.txt:This is A's file1. Line 2 in A.dir_B/file1.txt:This is B's file1. Line 2 in B.dir_A/subdir/file2.txt:Content of A's file2.dir_B/subdir/file2.txt:Content of A's file2. Additional line in B's file2.dir_B/unique_file_B.txt:Only in B.
If you run diff -r dir_A dir_B, the output would be:
diff -r dir_A/file1.txt dir_B/file1.txt
1c1
< This is A's file1.
---
> This is B's file1.
< Line 2 in A.
---
> Line 2 in B.
diff -r dir_A/subdir/file2.txt dir_B/subdir/file2.txt
2a3
> Additional line in B's file2.
Only in dir_B: unique_file_B.txt
Breaking down the output for file content differences:
diff -r dir_A/file1.txt dir_B/file1.txt: This line tells you which two files are being compared.1c1: (changed) Line 1 in the first file changed to line 1 in the second file.< This is A's file1.The line from the first file (dir_A/file1.txt).---A separator.> This is B's file1.The line from the second file (dir_B/file1.txt).
2a3: (added) Line 2 in the first file has a corresponding line added after line 3 in the second file. (In this specific example, it means after line 2 in the first file, something was added in the second file at line 3).> Additional line in B's file2.This line was added indir_B/subdir/file2.txt.
Only in dir_B: unique_file_B.txt: This indicates thatunique_file_B.txtexists indir_Bbut not indir_A.
So, diff -r essentially runs diff on every pair of similarly named files it finds across the two directory trees, and also reports files that exist in only one of the trees.
The format of diff output can sometimes be a bit challenging to read at first glance, but it's very precise. The key is to remember:
- Lines starting with
<are present in the first file/directory but not the second (or are different). - Lines starting with
>are present in the second file/directory but not the first (or are different). - Lines without
<or>context are lines that are identical and shown to provide context around the changes.
Did this detailed example clarify how diff -r handles file content differences for you?