14

is there a command line program that gives recursive word-based diff (on 2 directories)?

diff -u is recursive, but it doesn't do word by word comparison. wdiff and dwdiff does word based diff but there are not built-in options for recursive diff.

I would like to pipe the result to colordiff so a program that generates output that colordiff understands would be especially useful. Any suggestions? Thanks!

CC

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
ceiling cat
  • 5,501
  • 9
  • 38
  • 51
  • This doesn't really answer your question (hence posting as a comment), but if you're on a system with a GUI then it might be easier to use a GUI diff program. I find meld (http://meld.sourceforge.net/) to be quite good. – John Bartholomew Feb 13 '11 at 01:57

1 Answers1

28

Git can do it and output color:

The following often works:

git diff --color-words path1 path2

but in general you may need to do

git diff --no-index --color-words path1 path2

Neither file even needs to be in a git repository!

--no-index is needed if you and the paths are in a git working tree. It can be elided if you or one of the files are outside a git working tree.

Manpage: https://git-scm.com/docs/git-diff/1.8.5 (and later...)

git diff --no-index [--options] [--] […​]

This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.

Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
Michael W
  • 296
  • 4
  • 3
  • +1 - I never knew you could use git diff on files outside a repo. Thanks. – Russell Troywest May 05 '11 at 07:47
  • Great idea if both inputs are files, but what if one input is the output of a command? `-` isn't accepted as a directive to use stdin. I tried Bash process substitution (`git diff --no-index -- <(echo foo quux baz) somefile`), but apparently `git-diff-file` doesn't read from a named pipe but instead prints the "contents" as `pipe:[3377923]` or similar. – cjs Apr 25 '19 at 06:14
  • 1
    @cjs If your commands take a file as input, you may be able to use textconv. For example, I wanted to run `git diff ... <(gzip -dc foo.gz) <(gzip -dc bar.gz)`. I was able to achieve this by adding `*.gz diff=gz` to .gitattributes, adding `[diff "gz"]` ` textconv=gzip -dc` to .gitconfig, and running `git diff --textconv ... foo.gz bar.gz` – Ken Keys Apr 28 '20 at 18:02