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.
Great trick found by @climagic on Twitter, @osxdaily is on there too!