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!
Anybody know how to convert this to an Automator workflow that asks for the two folders?
diff -qr ~/Downloads/Pictures ~/Downloads/OldPictures
or just more simple
diff -qr ~/Downloads/{Pictures,OldPictures}
Neat. I would never have thought of using comm for this. The output format is a bit clunky, but very workable.
if you want to compare directories graphically: get FileMerge from Apple’s Developer Tools (which can be called from the command line using “opendiff”).
Nice one, thanks….
It would be nice to have the same tricks for 2 TXT files, i mean the “Dif” of the content inside the .txt file…with finaly the command for remove dup & sort, can be by the way ” sort -u”…, but the command for output & compare to files is ??????
Upss…sorry, my mistake. There shouldn’t be any space after < sign :
$ comm -3 <(ls -l Downloads ) <(ls -l Pictures )
Thanks for the tips
It doesn’t work on my Terminal :
ttirtawi@macbook-air:~$ comm -3 < (ls -l Downloads ) < (ls -l Pictures )
-bash: syntax error near unexpected token `('
ttirtawi@macbook-air:~$
Instead we could use TotalFinder.
yes, but that wouldn’t be the command line