How can I compare two directories with sub dirs to see where is the difference?

  • 6
    Operating System please. – Maximus Minimus Feb 9 '10 at 15:22
  • Are you wanting to know if there are different files between the two directories or if the contents of the files are different? – Matt Simmons Feb 9 '10 at 17:19
up vote 19 down vote accepted

Under Linux:

$ diff -r /first/directory /second/directory

Under Windows: you'd probably better download and install WinMerge, then

> WinMerge /r c:\first\folder c:\second\folder

M

  • 3
    I use diff -qrl now... – alexus Aug 10 '11 at 19:28
  • 1
    Is there an option in the GUI to specify the /r switch without using the command prompt? – Omtara May 7 '13 at 3:31
  • @Omtara, after you have started WinMerge, choose File - Open. In the open dialog, check //Include Subfolders//. If you open WinMerge by selecting two folders in the Windows Explorer, configure Shell integration. Open Edit - Options; navigate to Category //Shell Integration and check //Include subfolders by default//. – R. Schreurs Apr 8 '14 at 15:26

I used meld on Ubuntu - it has a good directory comparison option.

  • +1 for meld, normally I like command line options like diff but being able to visually see the actual different folders of two different directories at a glance in such a visual manner is very useful. Both meld and diff still work just fine on Ubuntu 16.04 and Ubuntu 18.04 for anyone else finding this in 2018. Of course on Windows, WinMerge is a great option. I have heard Meld works on Windows but I have not personally tried it yet. – Ken Apr 22 at 13:45

Beyond Compare is a good commercial tool, $30 or so. Runs under windows, has an eval version. http://www.scootersoftware.com/

On Windows, I believe windiff does it, however Winmerge is my tool of choice for this job. It's open source and does a very neat job of comparing two sets of directory trees.

edit: oops, was beaten to it by Marius

DiffMerge for Windows shows differences including subfolders in a window. There is also a portable version somewhere but a quick search revealed this download: http://www.softpedia.com/get/System/File-Management/SourceGear-DiffMerge.shtml

I wrote this using the Compare-Objects cmdlet in Powershell:

#set the directories 
$firstdirectory = Read-Host "What is the first directory you wish to compare?" $seconddirectory = Read-Host "What is the second directory you wish to compare?"

#Check if the user wants to compare subdirectories 
$recursivesearch = Read-Host "Do you wish to compare subdirectories? Please enter yes or no." If ($recursivesearch -eq "yes") 

#get the contents 
{ $firstdirectorycontents = @(Get-ChildItem $firstdirectory -Recurse) $seconddirectorycontents = @(Get-ChildItem $seconddirectory -Recurse ) }

    else { $firstdirectorycontents = @(Get-ChildItem $firstdirectory) $seconddirectorycontents = @(Get-ChildItem $seconddirectory) }
    #compare the objects and handle errors 
if ($firstdirectorycontents.Count -eq 0 )
        {
        Write-Host "No files were found in the first directory, the directories cannot be compared."
        }
        elseif ($seconddirectorycontents.Count -eq 0)
        {
        Write-Host "No files were found in the second directory, the directories cannot be compared."
        }
        else
        {   
        try 
            {
            Compare-Object -ReferenceObject $firstdirectorycontents -DifferenceObject $seconddirectorycontents 
            }

        catch {"Another error occured."} }

Diff is normally used to compare two files, but can do much more than that. In diff he options "r" and "q" make it work recursively and quietly, that is, only mentioning differences, which is just what we are looking for:

diff -rq todo_orig/ todo_backup/

If you also want to see differences for files that may not exist in either directory:

diff -Nrq dir1/ dir2/

You can also use Rsync and find. For find:

find $FOLDER -type f | cut -d/ -f2- | sort > /tmp/file_list_$FOLDER

But files with the same names and in the same subfolders, but with different content, will not be shown in the lists.

If you are a fan of GUI, you may check Meld. It works fine in both windows and linux.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.