comm command and diff command

compare-directory-contents

Compare Contents of Two Directories from the Command Line

 To compare and 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 command, adjusting the directory paths as appropriate:

comm -3 <(ls -1 folder1) <(ls -1 folder2)

The output listed will be the files that are different in each folder, with files unique to folder1 aligning left, and files unique to folder 2 aligning right.

For example, to compare the contents of a folder called “Pictures” and a folder named “OldPictures”, both stored in the user downloads directory, the syntax would be the following:

comm -3 <(ls -1 ~/Downloads/Pictures) <(ls -1 ~/Downloads/OldPictures)

Output may look like the following:

$ comm -3 <(ls -1 ~/Downloads/Pictures) <(ls -1 ~/Downloads/OldPictures)
Folder-1-File.PNG
Folder-2-File copy.PNG
  photo 1 copy.PNG
  photo 3.PNG

Note the indentation, which shows you which files are unique to each folder. In the above example, the file “photo 1 copy.PNG” and “photo 3.png” are aligned right, therefore they are unique to the OldPictures directory, and Folder-1-File.PNG and Folder-2-File copy.PNG are unique to the original Pictures folder.

This works great in Mac OS X, but it’s a generic unix command so you should find it usable in linux and other variants as well. If you do run into any compatibility issues, or find this command to be unnecessarily complex, try using diff to perform the same function.

Compare Two Directories Contents on a Mac Using diff

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

This command will work in Mac OS X as well as most Unix based OS’s.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: