Have you ever wanted a quick way to compare two directories (folders), in order to see which files may differ between the two? Back in 2006, we discussed FileMerge (part of Apple’s Xcode developer tools), which can do just that. There are other third-party GUI tools as well, but there’s actually a free folder comparison tool built into every Mac—it just requires a quick trip to Terminal to put it to use. The program is called diff, and it’s quite simple to use.
Launch Terminal (in Applications -> Utilities), and then use the cd command to change to the directory containing the folders you’d like to compare. (The folders can be located anywhere, of course, but it’s easiest if they’re in the same folder.). Once there, just run this command:
diff -rq folder1 folder2
This is a pretty simple command, with two command-line switches (-rq). The r tells diff to look at each directory recursively, including subdirectories. The q switch sets diff brief mode. If we didn’t set brief mode, diff would not only tell you which files are different between the two folders, but also show the actual line-by-line differences for any text files that exist in both locations but are not identical. Given that we’re just interested in comparing the folders’ contents, we don’t need that level of detail, so we’ll use brief mode to suppress it.
How to Use Diff to Compare Files at the Command Line The diff is a command line tool, thus you must first launch the Terminal app, found in /Applicaitons/Utilities/ and then you’re ready to begin.
The basic syntax for diff at the command line is as follows:
diff (file input 1) (file input 2)
For example, if in the present directory we want to compare bash.txt and bash2.txt, the syntax would look like the following:
diff bash.txt bash2.txt
The -w flag can be handy for plain text files because it tells diff to ignore white space when comparing files. And of course you can use a full path to the files to compare as well if need be, for example to compare an edited hosts file with another version elsewhere:
diff -w /etc/hosts ~/Downloads/BlockEverythingHosts.txt
Sample output may look something like the following:
$ diff -w /etc/hosts ~/Downloads/BlockEverythingHosts.txt
0a1
< ## < 127.0.0.1 localhost > # time for a break
9a12
> 127.0.0.1 facebook.com
The greater than and less than symbols serve as pointer arrows of sorts, indicating which file the difference originated from in relation to the order presented in the original command syntax.