Terminal: Comparing Folders with "diff"

Let's say you've copied an older version of a folder off of your backup drive, and you need to find out how its contents compare to the current version on your Mac. A lot of folks don't know that there's a built-in way to do that, and it's pretty easy to use and understand. It's accessed through the Terminal, so first you'll open that program from your Applications> Utilities folder.

When you've got it ready to go, type diff in at the prompt followed by a space, and then drag the two folders you want to compare and drop them on the Terminal window. It doesn't matter what order you do this in, and you don't have to type a space in between, either.

That'll make Terminal fill in the paths to those folders for you, and you should see something like this:

Press Return to implement the command, and your Terminal window will fill with delicious data.

Holy crap, that's a lot of gobbledygook. The thing is, though, that the gobbledygook is pretty interesting. From it, I can tell that I've got two JPEG files with the same name but different content:

Binary files /Users/melissa/Desktop/TMO/20130524 Table.jpg and /Users/melissa/Desktop/TMO Copy/20130524 Table.jpg differ

I see that one file only lives in the copy and is missing from the original folder:

Only in /Users/melissa/Desktop/TMO Copy: Account Info.jpg

And I also find that a text file has different content than its counterpart:

7c7,9
< In the window that appears, you'll configure your rule
\ No newline at end of file
---
> In the window that appears, you'll configure your rule
>
> text differs
\ No newline at end of file

(I typed “text differs” within one of them to show you how it'd look.)

So that's all pretty handy, but you have a few options you can add on, too, to make things even better. For example, you can use the “-r” option to force the command to search all subfolders, like so:

diff -r [path to folder 1] [path to folder 2]

Another often-used choice is “-q,” which'll output less-detailed text that only tells you whether files differ and not how they differ. This makes your results much easier to read! Of course, you can combine this with the “-r” option, too.

diff -rq [path to folder 1] [path to folder 2]

Additionally, you can use diff to compare files instead of folders by dragging a couple of those onto Terminal instead:

That tells me which lines are different and how.

Finally, just as with a lot of Terminal commands, you can send the results to a file instead of just into a Terminal window. This is often much easier to read and work with, but it does require you to be a bit familiar with things like paths. For this, you'll use the pipe (“>”) command, like so:

diff -rq [path to folder 1] [path to folder 2] > /Users/[username]/Desktop/comparison.txt

I know that looks complicated, but the English translation of it is “compare the two folders I've listed here, and send the results to a file on my Desktop called comparison.txt.” Note that if you use one pipe (“>”), as I've done above, that tells Terminal to overwrite a file that exists with that name, if any; if you use two pipes (“>>”), it asks Terminal to add to any existing files. So you could compare many different folders and pipe the results out to the same file, using the double-pipe every time to force it to add to your text rather than replacing it.

Neat, right? Not scary at all, is it? OK, Terminal's a little scary for some folks. I figure that certain people get reminded of playing text-based adventures like Zork way back when; you won't get eaten by a grue, I promise.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WIN an iPhone 16 Pro Max!