1

Is there an utility to compare the contents of a directory by file contents alone?

I have two sets of files, most of them binary same, but some of them different; but each files of a pair have different names even if their contents match.

Is there a tool that is able to compare the directory contents based on the actual file contents and does not require the filename to match?

I'm on Windows, but also interested in GNU/Linux tools.

n611x007
  • 6,566
  • 15
  • 67
  • 91

1 Answers1

0

This may work, I had some trouble with it, but I've dumped enough time into it that it's worth sharing in case it helps someone else.

Some caveats: Get-FileHash requires Powershell 4. This isn't doing anything with recursion.

$left = dir .\leftDir\* | Get-FileHash -Algorithm MD5
$right = dir .\rightDir\* | Get-FileHash -Algorithm MD5
$leftOnly = compare -passthru ($left | select hash) ($right | select hash)
$rightOnly = compare -passthru ($right | select hash) ($left | select hash)

echo "Print left only files"
foreach ($l in $leftOnly)
{
    foreach ($file in $left)
    {
        if ($file.Hash -eq $l.Hash) { echo $file.Path }
    }
}

echo ""

echo "Print right only files"
foreach ($r in $rightOnly)
{
    foreach ($file in $right)
    {
        if ($file.Hash -eq $r.Hash) { echo $file.Path }
    }
}
Jeff B
  • 4,997