1

I'm trying to recursively diff two directories using the command line diff util. I have two folders, diff1 and diff2, with contents like this:

enter image description here

diff2 is empty. But when I do diff -r diff1 diff2 I only get

$ diff -r diff1 diff2
Only in diff1: folder
Only in diff1: test.html

This is vexing. What do I need to do to get diff working recursively? I've tried --recursive, but that doesn't help. I'm on El Capitan, and a diff -v gets me diff (GNU diffutils) 2.8.1.

5
  • what would you expect output to be when one folder is empty? Nothing to diff really. It tells you whats in diff1. That is the difference. Have you tried putting a file in diff2? Or the file test.html with edited differences.
    – Natsfan
    Aug 25, 2017 at 17:38
  • Looks like it is working...
    – Natsfan
    Aug 26, 2017 at 14:53
  • @john, I'd expect the diff to recurse into the directory called folder - I mean what about another-folder, hello-txt and test2.html? When I read "recursively" I assume that the process won't stop at the first level.
    – user73784
    Aug 28, 2017 at 15:23
  • oh sorry, i missed that. Have you tried a capital R? as in -R
    – Natsfan
    Aug 28, 2017 at 15:25
  • 1
    no that won't work as there is no -R option. Sorry again. -r should work, I'm as baffled as you are... Unless the empty directory is confusing it somehow.
    – Natsfan
    Aug 28, 2017 at 15:32

1 Answer 1

0

The behavior you experienced is by design. I don't think you can get what you want, unless you allow tools other than diff. From your comment:

When I read "recursively" I assume that the process won't stop at the first level.

It doesn't stop because of the first level. Mind what recursion means:

using functions that call themselves from within their own code

(source: here; emphasis mine)

Imagine you have a common subdirectory foo in both diff1 and diff2. Recursion means that your original command diff -r diff1 diff2 at some point calls

diff -r diff1/foo diff2/foo

OK, in practice this is most likely done by a subroutine calling itself within a single process (but I guess an implementation that actually calls diff executable is possible). Still logically it's like this.

You have diff1/folder but there's no diff2/folder. How can you call diff to compare these? In other words, what should <unknown_name> be, so you get your desired output from the below form?

diff -r diff1/folder diff2/<unknown_name>

There is no <unknown_name> that fits. Recursive diff cannot descend to folder without turning into something like recursive ls or find. Being recursive ls or find is not its job.

Still you can query diff1/folder with ls -R or find (recursive by default), after you learn from diff that Only in diff1: folder.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .