How to Compare Two Directories Recursively in Ubuntu Terminal Using diff -r

Managing software releases, validating staging server deployments, and synchronising backup storage on Ubuntu often require verifying whether two separate folder trees contain identical files. When configurations or source code diverge, opening individual files manually to find discrepancies is inefficient. In Ubuntu, the native diff utility includes recursive scanning capabilities that systematically compare file hierarchies, flag files present in only one directory, and highlight line-by-line code differences.

Running a Basic Recursive Directory Comparison

The -r (or --recursive) flag instructs diff to descend through all subdirectories and nested folders:

  1. Launch your terminal via Ctrl + Alt + T or connect over SSH.
  2. Run the recursive diff command by specifying the two folder paths:
    diff -r /path/to/folderA /path/to/folderB
  3. If the directories are completely identical, diff silently returns to the command prompt with exit code 0.
  4. If differences exist, the command outputs which files exist in only one folder alongside line-by-line text modifications:
    Only in /path/to/folderA: config.backup.json
    Only in /path/to/folderB: new_feature.py
    diff -r /path/to/folderA/settings.ini /path/to/folderB/settings.ini
    12c12
    < port = 8080
    ---
    > port = 443

Displaying a Brief Summary Without Code Dumps (-q)

When comparing extensive directory trees containing thousands of files, printing full text diffs can flood your terminal buffer. Use the brief flag (-q) to list only which files differ without outputting code lines:

diff -rq /path/to/folderA /path/to/folderB

The terminal will print concise alerts stating “Files A and B differ” or “Only in folder: [filename]”.

Ignoring Whitespace and Line Ending Differences

When comparing folders transferred between Windows and Linux environments, carriage return line endings (CRLF vs LF) can cause every file to report false-positive differences. Add formatting flags to filter out stylistic variance:

  • Ignore Trailing Carriage Returns: Use --strip-trailing-cr to neutralise Windows line endings.
  • Ignore Whitespace Changes: Add -w to ignore all whitespace and tab indentation:
    diff -rqw --strip-trailing-cr /path/to/folderA /path/to/folderB
  • Side-by-Side Viewing: To inspect differences in a clean two-column comparison layout, use the -y flag with --suppress-common-lines:
    diff -r -y --suppress-common-lines /path/to/folderA /path/to/folderB

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.