Compare Two Directories Contents on a Mac Using diff

Jul 12, 2010 - 5 Comments

terminal If you want to see the difference between two folders on a Mac, launch the Terminal and read on, because we’ll show you how to compare two directories and the contents of those directories by using the Terminal to output a file containing the precise differences shown between two target folders.

To achieve this comparison, we’ll use the command line tool ‘diff’, it will easily compare the contents of any two directories by using the following syntax:

diff -rq directory1 directory2 >> differences.txt

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”), and then redirects the output of that command to a file named differences.txt. Here’s an example and how the actual printout will look:

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

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

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

5 Comments

» Comments RSS Feed

  1. 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.

  2. 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.

  3. […] 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 […]

  4. […] 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 […]

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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