Compare Two Directories Contents on a Mac Using diff

Jul 12, 2010 - 6 Comments

terminal If you want to see the difference between two folders on a Mac, or compare two directories contents, you can easily do so with the help of the powerful diff command.

This tutorial will show you how to compare two directories, and the contents of those directories, by using the Terminal. This command line approach will output a file containing the precise differences shown between two target folders.

To achieve this comparison, we’ll use the command line tool ‘diff’, diff is available on all Macs, along with linux and other unix operating systems, so this is effectively a cross-platform solution for comparing directories. Diff is quite simple to use for easily comparing the contents of any two directories, just follow along by using syntax detailed below.

How to Compare Contents of Two Directories with diff

To get started, launch the Terminal in Mac OS (found in /Applications/Utilities/) and then use the following command syntax:

diff -rq directory1 directory2

Hit return when you have specified the appropriate directories to compare. This executes the diff command comparing directory1 and directory2 (if you have a folder with a space in the file name, just put it in quotes like so: “folder one”). We are using the -rq flag because -r means it is recursive to include subdirectories, and -q simplifies the command output to only the differences shown.

Sample output of the command may look like the following:

$diff -rq directory1 directory2

Only in directory1: example221.txt

Only in directory1: SuperSecretDifferentFile.rtf

Only in directory2: AmazingScript.py

Only in directory2: MyFavoriteSong.mp3

Only in directory2: MyFavoriteSpecialMovie.mp4

You can also go a step further and redirect the output of that command to a file, let’s say it’s named differences.txt:

diff -rq directory1 directory2 >> differences.txt

Here’s an example and how the actual printout will look. Let’s say w’re comparing folders named “old music” and “new music”, and we want the command output showing the difference between those two directories in the file named “musicfolders.txt” then the following command syntax would be used:

diff -rq "old music" "new music" >> musicfolders.txt

Now look in the present working directory for the file you just created via outputting the diff command, in this case the file is musicfolders.txt and the contents can be viewed in any text editor, command line or otherwise. Opening the text file you’ll see something like this:

Only in old music: song1.mp3
Only in old music: song2.mp3
Only in old music: song3.mp3
Only in new music: instrumental1.mp3
Only in new music: instrumental1.mp3

If you want to view the file from the command line, try:

more musicfolders.txt

Otherwise just navigate to the containing directory and open it in your favorite text editor.

If you’d prefer not to create a text file with the changes, just leave off the output redirection of the command. You might want to pipe the output to something like ‘more’ to make it easier to scan though:

diff -rq "old music" "new music" | more

The diff command is quite powerful and there are many other options available with it, use the man diff command to get full details on how to use diff as well as the myriad features available.

It’s worth mentioning again that this command will work in Mac OS X – all versions – as well as most Unix based OS’s.

.

Related articles:

Posted by: David Mendez in Command Line, Mac OS, Tips & Tricks

6 Comments

» Comments RSS Feed

  1. […] list the different contents of two directories without the extra output you get through commands like diff, you can use the comm command instead. To get started, launch Terminal and type the following […]

  2. […] command line approach also lets you do things like compare two directory listings by using the diff command, that can either be done by comparing the output files against each other, or even directly […]

  3. Howard says:

    has this EVER worked for anyone who’s not a programmer ?

    • Milly says:

      it has worked for everybody who is capable to open the built-in application “Terminal” and then write “diff -rq” followed by the names of the two directories.

      For those unable to open applications or unable to write 7 characters in a specified sequence it has not worked.

    • Man, if you think this is what programmers do, you’ll be blown away with what we actually do.

  4. Brian says:

    To take this to the next step, for me, would be to move the different files to another directory or to simply highlight them in a finder window.

    I have 2 folders: one with TIFs and one with PDFs that were made from the TIFs. Not all the TIFs processed so I need to see which ones I need to run again. So ideally I only need to see the TIFs that don’t have a match in the PDF folder.

    Any help would be greatly appreciated.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site