To compare nested directories (directories that contain other folders and files) with diff, you use the -r (recursive) option.
As you just tried in the lab, the command structure looks like this:
diff -r directory1 directory2
How it works:
- Dives Deep: The
-rflag tellsdiffnot to stop at the first folder level. It will enter every sub-directory it finds and compare the contents there too. - Identifies Missing Files: It will report if a file exists in one directory tree but is missing from the other (e.g.,
Only in directory1/subfolder: secret.txt). - Shows Content Differences: If a file with the same name exists in both nested paths but the text inside is different, it will show you the exact line-by-line differences between those two files.
Pro Tip: Making it easier to read
When comparing large nested directories, the output can be very long. You can combine -r with other useful flags:
- Brief Output (
-q): If you only want to know which files are different without seeing the actual line changes, use:diff -rq directory1 directory2 - Report Identical Files (
-s): If you want to confirm which files are exactly the same, use:diff -rs directory1 directory2
You've already successfully used diff -r ~/Desktop ~/Code in your terminal! You can see from your output that it found files that exist "Only in" your Desktop folder.