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:
- Launch your terminal via
Ctrl + Alt + Tor connect over SSH. - Run the recursive diff command by specifying the two folder paths:
diff -r /path/to/folderA /path/to/folderB - If the directories are completely identical,
diffsilently returns to the command prompt with exit code0. - 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-crto neutralise Windows line endings. - Ignore Whitespace Changes: Add
-wto 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
-yflag with--suppress-common-lines:diff -r -y --suppress-common-lines /path/to/folderA /path/to/folderB