What is the best and simplest way to compare two directory structures without actually comparing the data in files. This works fine:

diff -qr dir1 dir2

But it's really slow because it's comparing files too. Is there a switch for diff or another simple cli tool to do this?

link|improve this question
By "directory structure", do you mean just the directory paths, or the paths of both directory and non-directory files? – intuited Jul 22 '10 at 6:26
Yes, folders and files. – Jonah Jul 22 '10 at 15:30
In that case you should remove the -type d option from @slartibartfast's answer, or check out my answer. – intuited Jul 22 '10 at 17:52
feedback

5 Answers

up vote 5 down vote accepted

The following (if you substitute the first directory for directory1 and the second for directory2) should do what you're looking for and swiftly:

find directory1 -type d -printf "%P\n" | sort > file1
find directory2 -type d -printf "%P\n" | sort | diff - file1

The fundamental principle is that it prints out all of the directories including subdirectory paths relative to the base directoryN directories.

This could fall down (produce wierd output) if you have carriage returns in some of the directory names but not others.

link|improve this answer
1  
+1... but I think you mean sort > file1 – Dan Jul 22 '10 at 3:39
Whoops, earlier comment got %P confused with %p. Cleverly hiding my tracks. These aren't the droids you're looking for. – intuited Jul 22 '10 at 6:20
feedback

I usually use rsync for this task:

rsync -nav --delete DIR1/ DIR2

BE VERY CAREFUL to always use the -n, aka --dry-run, option, or it will synchronize (change the contents of) the directories.

This will compare files based on file modification times and sizes... I think that's what you really want, or at least you don't mind if it does that? I got the sense that you just want it to happen faster, not that you need it to ignore the difference between file contents. If you do want it to not list differing files with identical names, I think the addition of the --ignore-existing option will do that.

Also be aware that not putting a / at the end of DIR1 will cause it to compare the directory DIR1 with the contents of DIR2.

The output ends up being a bit verbose, but it will show you which files/directories differ. Files/directories present in DIR2 and not in DIR1 will be prefaced with the word deleting.

For some situations, @slartibartfast's answer may be more appropriate, though you'll need to remove the -type d option to enable the listing of non-directory files. rsync will be faster if you've got a significant number of files/directories to compare.

link|improve this answer
feedback
vimdiff <(cd dir1; find . | sort) <(cd dir2; find . | sort)

will give you a nice side-by-side display of the two directory hierarchies with any common sections folded.

link|improve this answer
feedback
ls > dir1.txt

ls > dir2.txt

Then just diff the two lists.

link|improve this answer
It seems like the OP wants a heirarchy of paths. This will diff all files in the current directory. It's debatable, but possible, that he just wants directories; he might want filenames rather than the contents of files. – intuited Jul 22 '10 at 6:24
@intuited - you're right. I misread it. – MDMarra Jul 22 '10 at 13:16
feedback

I was just looking for solution for this problem. The solution that I liked the most was:

comm <(ls DIR1) <(ls DIR2)

It gives you 3 columns: 1 - files only in DIR1, 2 - files only in DIR2, 3 - files only in DIR3 For more details look at this blog post.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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