Compare Contents of Two Directories from the Command Line

Nov 8, 2012 - 8 Comments

Compare directory contents from 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-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!


Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks


» Comments RSS Feed

  1. DG says:

    Anybody know how to convert this to an Automator workflow that asks for the two folders?

  2. Dirk says:

    diff -qr ~/Downloads/Pictures ~/Downloads/OldPictures

    or just more simple
    diff -qr ~/Downloads/{Pictures,OldPictures}

  3. Theo Vosse says:

    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”).

  4. Tonny says:

    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 ??????

  5. Tedy says:

    Upss…sorry, my mistake. There shouldn’t be any space after < sign :

    $ comm -3 <(ls -l Downloads ) <(ls -l Pictures )

    Thanks for the tips

  6. Tedy says:

    It doesn’t work on my Terminal :

    ttirtawi@macbook-air:~$ comm -3 < (ls -l Downloads ) < (ls -l Pictures )
    -bash: syntax error near unexpected token `('

  7. Alex says:

    Instead we could use TotalFinder.

Leave a Reply


Shop on and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks


iPhone / iPad



Shop on Amazon to help support this site