32

Doing: diff -r -X <ignore-list> <src-dir> <dest-dir>

doesn't seem to make diff ignore entries in <ignore-list> if they are of the form <dir>/<file>.

Entries of the form <file> do however get considered. This is a problem since I might have multiple files named <file> in different sub-directories, some of which I don't want ignored.

There doesn't seem to be much information regarding pattern syntax in the manpage for diff either. From what I can tell, it's just the base-name of a file that is considered by diff (see http://forums.gentoo.org/viewtopic-t-889788-start-0.html if you're interested).

1
34

Listing directories should work; e.g., here's what I've used in a script (assuming gnu diff),

diff -r \
   --exclude="*~" \
   --exclude=".svn" \
   --exclude=".git" \
   --exclude="*.zip*" \
   --exclude="*.gz" \
   --exclude="*.tar" \
   ...etc

...which ignores contents of .svn and .git dirs, but also individual files named *.zip/*.gz/etc.

Edit: In order to filter paths of the form dir_a/file1 but still diff files with the same basename, such as dir_b/file1 or dir_a/b/file1, then a list of files to diff would have to be generated (for example, using find), and the file to compare derived from these paths; e.g., given

$ find ONE TWO -type f -print 
ONE/a/1.txt
ONE/a/2.txt
ONE/a/b/2.txt
TWO/a/1.txt
TWO/a/2.txt
TWO/a/b/2.txt

you generate the list of files to compare, excluding for example */a/2.txt but still comparing other files named 2.txt. Just "find" all files except ONE/a/2.txt (a regexp can also be used here, such as .*/a/2.txt)

$ find ONE -type f \( ! -regex 'ONE/a/2.txt' \) \
    -exec bash -c 'diff -q "${1}" "${2/ONE/TWO}"' - {} {} \;  

which in effect ignores ONE/a/2.txt (and TWO/a/2.txt), but still compares the other files named 2.txt:

diff -q ONE/a/1.txt TWO/a/1.txt
diff -q ONE/a/b/2.txt TWO/a/b/2.txt

Edit: Or, more fun with find (additional fun left as an exercise for the reader), select the files or directories to exclude and then diff everything else:

$ find ONE \( -regex 'ONE/a/2.txt' -o -name b  -prune \)  \
    -o -type f -exec bash -c 'echo diff -q "${1}" "${2/ONE/TWO}"' - {} {} \

The above example excludes the specific file "{top}/a/2.txt", any directory named "b", and everything else is diff'd. (Instead of simple "-name b" you could also use "-regex '.*/b'" - note, no trailing "/".)

14
  • 2
    Thanks but I think you're missing the point. The only support there seems to be is when you use a 'base-name'. That could be the name of a directory or a file. In either case, diff ignores what you've asked for. The problem arises when you use paths. For example, I can't get diff to ignore /an/absolute/path/to/a/file or ./a/relative/path/to/a/file. – Ash Sep 13 '13 at 6:52
  • 2
    diff --exclude="/this/specific/file/that/im/explicitly/pleading/you/to/ignore". It won't work. – Ash Sep 13 '13 at 6:54
  • 4
    correct, exclude patterns are matched against the files' basename (as per gnu.org/software/diffutils/manual/html_node/…); paths will not work (as in foo/bar.txt). To do that, you will likely have to run find to generate the list of filenames, and derive the path to the file to compare. – michael Sep 14 '13 at 11:17
  • 1
    Glad to hear you've got something working, but... for posterity: "you've passed the type -f to find. If you leave this out, the result might contain directories" => correct: so, don't leave it out. I added it for a reason. No need to recursive diff if you're already using recursive find. "I need find to list directories" => no, you don't; you need the files in directories to be listed, and find does that. "No option to exclude symlinks" => using -type f does exclude symlinks; alternatively, use -type l -prune -o will exclude symlinks (use in my example w/ patterns to exclude) – michael Sep 19 '13 at 5:51
  • 1
    true, could have saved a lot of time. just install rsync :-) (seriously, just install rsync) – michael Sep 20 '13 at 1:11
2

To exclude directory directory/sub-directory, I use

diff -r <src-dir> <dest-dir> | grep -v directory/sub-directory

However, although it should work for a single exclude, it should not be possible for a long ignore list as you have.

2

I had the same problem so I created a patch to diff. The patch has yet to be accepted, but you can build your own version of diff with the patch or install on Arch Linux with an AUR package.

Here is the diff patch.

-2
$ diff -rq foo.orig foo | grep -vP 'ignore1/|exclude2/' | awk '{print $2}' | cut -d'/' -f2- | xargs -I{} diff -u foo.orig/{} foo/{}
1
  • 1
    While this may answer the question, it would be a better answer if you could provide some explanation why it does so. – DavidPostill Jun 28 '17 at 7:45

Your Answer

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

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