Terminal: Comparing Folders with “diff”

| TMO Quick Tip

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.

Comments

furbies

Melissa, once again thanks for another great tip.

I just wish I could remember these tidbits when I need them….

rjackb

Thanks for the tip, Melissa. For something more visual, which makes difference comparisons a lot easier for me, I use either Xcode (if you happen to have it installed) or there is a free Mac OS X app available called DiffMerge.

wab95

Melissa

You should compile all of your tips into a booklet, and sell it on the iBook Store.

I’ll buy it. One copy sold already, in advance of print.

Melissa Holt

Thanks for the comments, guys! I appreciate the nice words.

wab95, that’s a great idea—maybe I should discuss it with the TMO brass. You’re wonderful for saying so, too! grin

—Melissa

furbies

I’d like to +1 for wab95’s idea.

Melissa, you really should do an iBook. Just so long as it’s available in Australia…..

Melissa Holt

Aw, thanks, furbies! How sweet of you.

JasperR

Hi - thanks for this - it’s the nearest thing to my needs that I’ve found so far, and nice & easy to follow smile

So my question is: Is there a similarly cool way to flag up sub-folders with the same name when they occur in a single top-level folder?

So I’d like to use Terminal to look inside a single top-level folder (iTunes Media Library folder) and flag up sub-sub-folders with the same names that will be located in sub-folders (artist name).

Thank you!

 

Melissa Holt

Hey JasperR,

I’m not personally aware of how to do that with diff (although there may be a way to do so, and hopefully someone with more knowledge than I can chime in); however, I use a nifty little utility called Print Window Advanced (http://www.searchwaresolutions.com) when I need to do some fancy tweaking to print out a proper list of filenames with subfolders and sub-subfolders. For example, here’s a list I did of my music folder, and it would be easy to see everything that fell within the same artist’s name:

http://i.imgur.com/fuLCVYc.png

I really like Print Window Advanced, but you could try out their free version to see if the program works for what you’re looking to do before you pay for it.

I hope that helps in some small way, and thanks for reading!
Melissa

JasperR

Thanks Melissa - that’s very kind of you to make this suggestion & I hadn’t thought of this.

I’d made (but entirely forgotten about) an Automator Workflow that does the same thing. Here’s a picture of its ingredients for you to duplicate if you want to:

https://www.dropbox.com/s/p114qkzvn51uojo/MacObserverPost19Nov14.png?dl=0

Best wishes, Jasper

PS - I haven’t heard Momentary Lapse of Reason since the late 1980s - can it really be that long ago?!

Melissa Holt

Hey JasperR!

That is an excellent workflow! Thanks for sharing it with me. You rock!

“PS - I haven’t heard Momentary Lapse of Reason since the late 1980s - can it really be that long ago?!”

Sadly, all of the good music was at least that long ago. wink

—Melissa

Log-in to comment