978

If I want find the differences between two directory trees, I usually just execute:

diff -r dir1/ dir2/

This outputs exactly what the differences are between corresponding files. I'm interested in just getting a list of corresponding files whose content differs. I assumed that this would simply be a matter of passing a command line option to diff, but I couldn't find anything on the man page.

Any suggestions?

3

10 Answers 10

1409

Try:

diff --brief --recursive dir1/ dir2/

Or alternatively, with the short flags -qr:

diff -qr dir1/ dir2/

If you also want to see differences for files that may not exist in either directory:

diff --brief --recursive --new-file dir1/ dir2/  # with long options
diff -qrN dir1/ dir2/                            # with short flag aliases
12
  • 29
    Nice. But shorter is diff -qr dir1/ dir2/ and my extended version to diff -qr dir1/ dir2/ | grep ' differ'
    – sobi3ch
    Aug 7, 2015 at 13:18
  • 3
    @skv Not exactly what the original question asked, but updating the answer to accommodate this question as well. Dec 9, 2015 at 19:43
  • 12
    @MikeMaxwell It needs to be --brief. -brief is interpreted as -b -r -i -e -f, in other words as a set of flags not as a single option.
    – daboross
    Oct 25, 2018 at 4:27
  • 3
    @daboross: wow, I've been using Unix/Linux for a l o n g time, and I never realized there was that distinction between '--' and '-'. (I don't think '--' existed when I got started.) Thanks for the explanation! Oct 30, 2018 at 16:37
  • 10
    - options are called "UNIX options" and -- options are called "GNU long options" according to man ps. You should make every program accept long options if it uses any options, for this takes little extra work and helps beginners remember how to use the program. source: gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html, also google.com/search?q=gnu+long+options Feb 22, 2019 at 16:39
315

The command I use is:

diff -qr dir1/ dir2/

It is exactly the same as Mark's :) But his answer bothered me as it uses different types of flags, and it made me look twice. Using Mark's more verbose flags it would be:

diff  --brief --recursive dir1/ dir2/

I apologise for posting when the other answer is perfectly acceptable. Could not stop myself... working on being less pedantic.

7
  • 13
    ..so does it make sense tu put different answers with JUST a different flavour? IMHO no! Does it make sense tu combine both answers to one consistent answer? yes! ;)
    – sobi3ch
    Aug 7, 2015 at 13:21
  • 1
    Just a question; what does the q stand for? Is it an abbreviation of something? I can't find any logic behind the q..
    – kramer65
    Nov 5, 2016 at 22:26
  • 3
    @kramer65 - it is the same as "--brief", but I guess you wonder why q? Perhaps for quick? "-b" is taken by "ignore changes in the amount of white space" according to the man page.
    – FPC
    Nov 23, 2016 at 10:27
  • @sobi3ch You are right, I apologise again. To my defence, I do not think I had the ability to edit the other answer at the time.
    – FPC
    Nov 23, 2016 at 10:30
  • 9
    @kramer65 I believe the q is for quiet, generally meaning less verbose.
    – Gogeta70
    Aug 15, 2017 at 17:26
155

I like to use git diff --no-index dir1/ dir2/, because it can show the differences in color (if you have that option set in your git config) and because it shows all of the differences in a long paged output using "less".

5
  • 46
    Neat. Who would've guessed that git can diff arbitrary directories, not just the repo against its files? May 6, 2014 at 0:34
  • 3
    Perl script colordiff is very useful here, can be used with svn and normal diff. May 15, 2014 at 2:37
  • 8
    If you comparing (like me) 2 dirs as seperate git projects/repos then you need add --no-index more on stackoverflow.com/a/1792477/473390. I've updated @alan-porter answer.
    – sobi3ch
    Aug 7, 2015 at 13:30
  • 1
    I like this one, I also find that if you add --name-status to the command line, it will just show the file name list with "M/A/D" flags for Modified/Added/Deleted status.
    – gzh
    Feb 27, 2020 at 6:24
  • 1
    It happens so that both directories are actually containing the .git folder, how can I exclude it from the compare?
    – Silidrone
    Mar 28, 2020 at 16:37
58

Using rsync:

rsync --dry-run --recursive --delete --links --checksum --verbose /dir1/ /dir2/ > dirdiff_2.txt
# or same in short
rsync -nrlcv --delete /dir{1,2}/ > dirdiff_2.txt

Alternatively, using diff:

diff --brief --recursive --no-dereference --new-file --no-ignore-file-name-case /dir1 /dir2 > dirdiff_1.txt
# or same in short
diff -qrN --no-dereference --no-ignore-file-name-case /dir{1,2} > dirdiff_1.txt

They are functionally equivalent, but performance may vary depending on:

  • If the directories are on the same drive, rsync is faster.
  • If the directories reside on two separate drives, diff is faster.

This is because diff puts an almost equal load on both directories in parallel, maximizing load on the two drives. rsync calculates checksums in large chunks before actually comparing them. That groups the i/o operations in large chunks and leads to a more efficient processing when things take place on a single drive.

9
  • 5
    rsync is not only faster for files on single drives, but also allowes for comparing files in subdirs, for example rsync --options /usr /bin /var /sbin /lib /old_root will effectively compare current root / (by specifying all subdirs in it) and /old_root (containing for example some older backup of /), which is something diff -r can't do. And if you assume that files with same size, permissions and timestamps probably have not changed, leaving out --checksum will provide you with extremely fast (if not so through) check of which files might have changed. Aug 19, 2017 at 21:52
  • 3
    The purpose of --delete is to delete existing files in destination-dir which are not (any longer) present in source-dir Sep 8, 2017 at 9:31
  • 7
    In this case (with the --dry-run flag) nothing is really deleted, rsync only prints which files are in dir1 but not in dir2
    – mata
    Oct 2, 2017 at 7:15
  • 22
    I'd recommend putting --dry-run first always as to not accidentally forget it.
    – Dave Rager
    Apr 12, 2018 at 19:41
  • 3
    The rsync solution is very useful if you need to compare a local with a remote directory accessible over ssh May 22, 2019 at 11:52
41

Meld is also a great tool for comparing two directories:

meld dir1/ dir2/

Meld has many options for comparing files or directories. If two files differ, it's easy to enter file comparison mode and see the exact differences.

7
  • 2
    Nice. I have written a simple perl script to perform comparison over trees but I am hitting limitations. This seems to be the ticket. May 19, 2017 at 11:44
  • The only problem is that it does not lend itself to scripting since it is a graphical app. But it is nice if you don't mind the GUI! Thanks.
    – DeanM
    Dec 8, 2018 at 18:14
  • 1
    I find that meld becomes horribly sluggish if used on large directories though. Is there anything that handles large directories better?
    – Popup
    Sep 6, 2019 at 10:17
  • @Popup, not that I know of. You could find differing filenames with something like this, though: find dir1 dir2 | cut -d/ -f2- | sort | uniq --unique
    – Alexander
    Sep 6, 2019 at 10:26
  • 2
    @Alexander - In that case I find that meld <(find dir1 -ls ) <(find dir2 -ls) works pretty well, using bash process substitution. (zsh's =(command) works even better.)
    – Popup
    Sep 10, 2019 at 8:31
12

Channel compatriot 'billings' (of freenode/#centos fame) shared his method with me:

diff -Naur dir1/ dir2

Including the final directory forward slash doesn't matter.

Also, it appears the -u option is not available on some older/server versions of diff.

The difference in diffs:

# diff -Nar /tmp/dir1 /tmp/dir2/
diff -Nar /tmp/dir1/file /tmp/dir2/file
28a29
> TEST

# diff -qr /tmp/dir1/ /tmp/dir2/
Files /tmp/dir1/file and /tmp/dir2/file differ
1
  • 2
    So that's --new-file/-N which makes diff consider missing files to be empty and --text/-a which causes it to consider all binary input to be text. I don't see the upsides for this particular use case.
    – phk
    Oct 7, 2016 at 21:39
5

To find diff use this command:

diff -qr dir1/ dir2/

-r will diff all subdirectories too -q tells diff to report only when files differ.

diff  --brief dir1/ dir2/

--brief will show the files that dosent exist in directory.

Or else

we can use Meld which will show in graphical window its easy to find the difference.

meld  dir1/ dir2/
1
  • 4
    --brief and -q are the same option. Your statement makes it sound like they are different but they aren't. Feb 22, 2019 at 16:36
4

Diffoscope is a great command line based directory diff tool.

I especially like about it that it can diff into files:

It will recursively unpack archives of many kinds and transform various binary formats into more human readable form to compare them. It can compare two tarballs, ISO images, or PDF just as easily.

It will not only tell you which files differ, but also how they differ.

2

You can also use Rsync and find. For find:

find $FOLDER -type f | cut -d/ -f2- | sort > /tmp/file_list_$FOLDER

But files with the same names and in the same subfolders, but with different content, will not be shown in the lists.

If you are a fan of GUI, you may check Meld that @Alexander mentioned. It works fine in both windows and linux.

2

To report differences between dirA and dirB, while also updating/syncing:

rsync -auv <dirA> <dirB>
2
  • 1
    While it may work, using rsync is adding a layer of complexity, because now you need that dependency. It is a nice collateral, but is uses a little more than just linux in my opinion.
    – Lomefin
    Dec 27, 2020 at 18:38
  • 1
    @Lomefin I don't see how rsync is less Linux than diff. @Kickaha You definitely want a backup of your target directory, before launching that command. Dec 28, 2020 at 1:25

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