A Basic Directories Diff
Need to find out if two directories contain the same files? Here’s how to do it in a basic fashion. As a GitHub Gist.
Playing it Safe
When working with my precious photos, I like to play it safe. Work on a copy of the original photos. But this also means that at some point it’s time to clean up. Especially, since all storage media I own tends to have a finite capacity.
The question I usually find myself facing then is
Now, these two directories
one
andtwo
— do they contain the same photos?!
Obviously, I’m not going to do a manual comparison. I’m usually just too trigger happy for that in the first place. Plus, a machine is a lot better at this than I could ever be.
A Poor Man’s Directories Diff
Luckily, macOS comes with all the tools needed to compare if directories one
and two
contain the same files.
Here, the idea is to generate the list of files for both directories
one
andtwo
and then compare them usingdiff
.
Now, why is this “A Poor Man’s Directories Diff”?!
Well, the diff is rather basic. It does what it says on the tin. Simply compares file names and then tells you any differences. By design, there is no inspection of the files themselves. This means that one/photo.jpg
could be entirely different from two/photo.jpg
while the diff declares them to be indistinguishable.
Plus, the diff is more useful for humans when there are a limited number of differences between the two directories. Once the differences reach into the hundreds or thousands, then the diff is probably best post-processed by a machine. Well, at least in that case you know, that there’s a not so small difference.
Everything in One Go
When wrapping the above idea with some input parameter handling and favourable output formatting for humans, the result is a single Bash script in the following GitHub Gist
So, How do You Diff Directories?!
While the above Worx for Me!™ when it comes to diff’ing directories, you may have an alternative or better way.
Think this is all rubbish, incomplete, massively overcomplicated, or simply the wrong tool for the job?! Feel free to leave a comment or contribute to the GitHub Gist or reach out to me on LinkedIn and teach me something new!
As always, prove me wrong and I’ll buy you a pint!
Subscribe to How Hard Can It Be?!
Get the latest posts by following us on LinkedIn and Twitter