How to compare nested directories with `diff`?

QuestionsQuestions8 SkillsProDec, 24 2025
0103

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:

  1. Dives Deep: The -r flag tells diff not to stop at the first folder level. It will enter every sub-directory it finds and compare the contents there too.
  2. 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).
  3. 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.

0 Comments

no data
Be the first to share your comment!