17

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

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
alexus
  • 12,342
  • 27
  • 115
  • 173

7 Answers7

22

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

MariusPontmercy
  • 677
  • 4
  • 15
  • 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 07 '13 at 03: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 08 '14 at 15:26
2

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

slm
  • 7,355
  • 16
  • 54
  • 72
DNayak
  • 21
  • 1
  • +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 '18 at 13:45
2

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.

Fábio
  • 121
  • 4
1

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

1

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

Bryan
  • 7,538
  • 15
  • 68
  • 92
0

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

Daniel Serodio
  • 249
  • 3
  • 10
GHad
  • 109
  • 4
0

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."} }
Davidw
  • 1,210
  • 3
  • 14
  • 24