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

|-- 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


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?


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:

When comparing directories, ignore files and subdirectories whose basenames match PATTERN.

  • 1
    thanks for pointing "The pattern is applied to the basename of files and directories, not to pathnames". its very helpful. – Brain90 Jul 9 '19 at 7:17

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.