19

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

alexus
  • 13,667

7 Answers7

25

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
slm
  • 8,010
4

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
  • 141
  • 4
2

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

slm
  • 8,010
DNayak
  • 21
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

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

GHad
  • 109
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,267