Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

1 Answer

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy