45

I have two directories containing source files to a project I've inherited with little by way of documentation. How do I compare both directories to make see what the differences are?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
freakwincy
  • 1,257
  • 2
  • 11
  • 16
  • With all the right answers provided above, if you need any video help I found this useful video in my search for answer https://www.youtube.com/watch?v=TcJkLV1EeuU – Vishwanath gowda k Nov 19 '14 at 13:52

5 Answers5

60

Try this:

diff -Naur dir1/ dir2/
  • The -u option makes the output a little easier to read.
  • The -r option recurses through all subdirectories
  • The -N and -a options are really only necessary if you wanted to create a patch file.
jcrossley3
  • 11,576
  • 4
  • 31
  • 32
  • As an human-readability enhancement to this, you can pipe to a tool like [bat](https://github.com/sharkdp/bat/) and get diff syntax highlighting: ```bash diff -Nur dir1/ dir2/ | bat -l diff ``` – treehead Jul 11 '23 at 15:30
36

You can try Meld. It is a wonderful visual diff tool ;-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
manatlan
  • 1,091
  • 1
  • 9
  • 18
  • Thanks a bunch- I discovered Meld belated after having posted my question and it does pretty much what I want. It would be nice if I didn't have to download the code to my local machine though as its on a remote server and (unfortunately) not checked into like CVS or Subversion. – freakwincy Apr 22 '09 at 12:25
  • Meld lets you see the diffs in the tree structure as well as line-by-line diffs in files. In addition, you can selectively apply changes in one directory to the other. Magnificent! – 18446744073709551615 May 11 '12 at 06:13
  • I had actually used Meld for file comparisons already, but I was not aware it was this great for tree comparison. – Thomas Arildsen Apr 08 '13 at 11:55
29
diff -u -r dirA dirB

Will show you a unified recursive diff between the files in dirA and dirB

Bert Huijben
  • 19,525
  • 4
  • 57
  • 73
7

You may use the diff command in the shell. Or install a tool like KDiff3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Decio Lira
  • 1,821
  • 1
  • 18
  • 24
  • 2
    +1 for KDiff3. Works a treat for comparing directories (and subdirectories etc.) – David Webb Apr 22 '09 at 12:03
  • +1 - I'd looked at KDiff3 but hadn't realised it did it due to me using drag-and-drop. You *can* drag-n-drop directories, but you'll need use the open dialog and hit OK for it to start the compare. – Deebster Jun 11 '12 at 12:51
1

The diff command to compare directories kept telling me that I didn't have differences, when I knew there were differences.

Instead of using diff directly, I used a sorted list of md5sums and then compared those files with diff:

find /path1/dir/ -type f -exec md5sum {} + | awk '{print $2 $1}' | sort >! path1.log
find /path2/dir/ -type f -exec md5sum {} + | awk '{print $2 $1}' | sort >! path2.log
gvimdiff path1.log path2.log

If the beginning part of the path is causing headaches, then change it. Select the Path1 window and type:

:%s|path1|path2|g

This will replace all instances of path1 with path2 in the first file, and now your diff should only show differences.

Sooth
  • 2,834
  • 23
  • 26
  • 1
    I've never seen `>!` before. What does it do? – Daniel Serodio Aug 13 '14 at 18:41
  • I'm just going to assume the `>!` bit is a typo and that it should read `>`. That way the command and process makes sense. – Jostein Kjønigsen Oct 01 '14 at 08:41
  • 1
    +1 assuming the `>!` is changed to `>`; the other minor variation I did was simply change directory to `path1/dir` and do `find .` so I wouldn't have to manually change the pathnames – mpontillo Nov 08 '14 at 01:32
  • 1
    Actually the >! is a csh redirect that will overwrite existing files. See: http://stackoverflow.com/questions/6762348/what-do-and-do-in-tcsh – Sooth Feb 10 '15 at 19:50
  • This seems to assume there are only two fields - ie, that filenames do not contain whitespace characters. Any work-arounds? – Tom Hale Jul 11 '17 at 02:56
  • 1
    @TomHale I would use sed to deal with spaces in paths: `find /path1/dir/ -type f -exec md5sum {} + | sed 's/\([^\ ]\+ \)\(.*\)/\2 \1/' | sort >! path1.log` The `sed` command is looking for a string with no spaces, and then it puts that string at the end, ignoring all other spaces. – Sooth Jul 12 '17 at 20:26