I want to use Diff only to check if files and directories exist the same in two locations but NOT compare the contents of the files themselves, because that's all I need and a regular Diff just takes too long for the amount of data. How would I go about this? Is there some other Debian standard tool that can accomplish this?
2 Answers
You can't use diff for that. Why would your requirement be to use diff? Why do people always come to conclusions without having examined the possible solutions in detail?
You could use diff -qr
but that wouldn't be wise from a performance point of view if the only goal is to compare the directory structure as outlined here
One of the answers to that question was
vimdiff <(cd dir1; find . | sort) <(cd dir2; find . | sort)
which
will give you a nice side-by-side display of the two directory hierarchies with any common sections folded.
-
1Links are useful for references, but they are no substitute for answering the question. If an answer would not be useful without the links, it is subject to deletion. – kasperd Apr 04 '16 at 13:36
-
Should be moved to superuser anyway where it can be flagged as duplicate because the answer is there. No need for me to take credit for answers other people gave there. – Marki Apr 04 '16 at 20:32
-
1No. That a similar question was asked on [su] does not make the question off-topic on [unix.se]. It also doesn't reduce the requirements for an answer to be considered suitable. – kasperd Apr 04 '16 at 20:39
-
1Wow, what a useful command! And kudos for OP for asking - had the exact same question even after looking through the manual of diff. Rant could be removed from answer IMO – dmeu Aug 10 '18 at 07:49
-
As I wrote in my answer. diff is the wrong tool. Why read the manual for diff instead of finding the right tool in the first place. – Marki Aug 10 '18 at 08:48
I would have made this a comment on Marki's answer but it would have been too long. There is a caveat to his solution:
Parsing output of ls
or find
are alike non-robust and liable to breakage. Here is an example:
$ mkdir dir{1,2}
$ touch !$/file{1..5}
touch dir{1,2}/file{1..5}
$ mkdir dir1/$'\n'.
$ touch !$/whoops
touch dir1/$'\n'./whoops
$ touch dir2/whoops
$ touch dir1/onlyin1
$ touch dir2/onlyin2
$ comm <( cd dir1 ; find . | sort ) <( cd dir2 ; find . | sort )
.
.
./
./
./file1
./file2
./file3
./file4
./file5
./onlyin1
./onlyin2
./whoops
(I'm using comm
for three-way text comparison rather than vimdiff
so I can copy-paste more easily; the result is the same in vimdiff
.)
You see that this incorrectly displays that the file whoops
is in both directories, when in fact one of those whoops
files is in in a subdirectory of dir1
which contains a newline in its name.
Normally people don't put newlines in filenames or directory names, and the vimdiff
answer should work on any other special characters (though I haven't tested). However it's still something to be wary of. If you are going to put this into a script or into production code of any kind, please work on making it more robust, e.g. by walking both directory trees properly and comparing them.