61

I'm trying to use diff to compare two directory trees while ignoring certain sub-directories, but I cannot get it to work. This is what my file structure looks like:

temp/
|-- d1/
|   |-- f1.txt
|   `-- ignoreme/
|       `-- f2.txt
`-- d2/
    |-- f1.txt
    `-- ignoreme/
        `-- f2.txt

I'm trying to get it to ignore anything under d1/ignoreme and d2/ignoreme, but it won't do it.

diff -qr --exclude=/home/ubuntu/temp/d1/ignoreme/ d1 d2
Files d1/ignoreme/f2.txt and d2/ignoreme/f2.txt differ

I also tried

diff -qr --exclude=/home/ubuntu/temp/d1/ignoreme/* d1 d2

and

diff -qr --exclude=/home/ubuntu/temp/d1/ignoreme/* --exclude=/home/ubuntu/temp/d2/ignoreme/* d1 d2

... but I always get the same result.

How do I get this to work?

1

1 Answer 1

77

The -x or --exclude options for GNU diff take a filename globbing pattern that the name of each file and directory will be matched against. If the pattern matches a particular name, that name is excluded from comparison.

The pattern is applied to the basename of files and directories, not to pathnames, which means that to exclude your ignoreme directory, you would use

diff -qr --exclude=ignoreme ...

This would also exclude any other name that happens to be ignoreme.

This is similar to the way that --exclude and --exclude-dir works in GNU grep when running grep recursively, although the GNU grep manual explains this better.

The info documentation for GNU diff spells it out:

-x PATTERN
--exclude=PATTERN
When comparing directories, ignore files and subdirectories whose basenames match PATTERN.

3
  • 9
    thanks for pointing "The pattern is applied to the basename of files and directories, not to pathnames". its very helpful. Commented Jul 9, 2019 at 7:17
  • i am trying to compare 2 web app using: " diff -rq /opt/tomcat10/webapps/app1 /opt/tomcat9/webapps/app1 --exclude=lib --exclude=class " but it seems to keep listing the .class and lib-provided Commented Nov 29, 2024 at 10:23
  • 1
    @Malkavian If the files/directories are not called exactly lib and cache, then use a pattern, e.g. --exclude='*.cache' or --exclude='*cache' or something similar (remember quote the pattern). Commented Nov 29, 2024 at 10:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.