diff

From NetBSD Wiki

Jump to: navigation, search

The diff(1) utility displays the difference between two text files specified on the command line.

By default, for each different line, line numbers in both files are displayed, separated with c. This is followed by < and the line in the first file, ---, and > and the line in the second file.

$ cat a.txt
line 1
line 2
line 3
line 4
line 5
line 7
line 8
line 9
line 10
$ cat b.txt
line 1
Line 2
line 3
line  4
line 5
line 6
line 7
line 8
line 10
$ diff a.txt b.txt
2c2
< line 2
---
> Line 2
4c4
< line 4
---
> line  4
5a6
> line 6
8d8
< line 9

Output formats

There are three output formats:

  1. The plain one, as seen in the introduction
  2. Copied context diff (-c option)
  3. Unified context diff (-u option)

The first format is quite obfuscated and not very informative. It only shows the lines that are removed and which are inserted, with their line numbers. The other two formats include a few lines of context around the affected lines. This allows patch to find the affected lines, even if the file has changed and lines have shifted up or down a bit because of editing (which happens all the time in source files).

Copied contextual output looks like this:

$ diff -c a.txt b.txt
*** a.txt       Wed Jun 14 21:34:44 2006
--- b.txt       Wed Jun 14 21:34:12 2006
***************
*** 1,9 ****
  line 1
! line 2
  line 3
! line  4
  line 5
  line 7
  line 8
  line 9
- line 10
--- 1,9 ----
  line 1
! Line 2
  line 3
! line 4
  line 5
+ line 6
  line 7
  line 8
  line 10

As you can see, this output format shows both the original file and the new one, with markings that indicate changed lines. This output is extremely verbose, especially if there are a lot of changes with long lines.


$ diff -u a.txt b.txt
--- a.txt       2006-06-14 21:40:22.000000000 +0200
+++ b.txt       2006-06-14 21:40:16.000000000 +0200
@@ -1,9 +1,9 @@
 line 1
-line 2
+Line 2
 line 3
-line  4
+line 4
 line 5
+line 6
 line 7
 line 8
-line 9
 line 10

This format is a bit of a middle ground between the (usually unnecessary) verbosity of copied context output (-c) and the (too extreme) brevity of plain diff output. Patch can handle this output quite well.

The preferred diff format for problem reports (send-pr) is unified diff. (citation needed) It is also the format used in pkgsrc's patch files, and output by pkgtools/pkgdiff.

See also

View source code (Please report any bugs or suggestions here).

NetBSD uses GNU diff. If you are able to write a BSD version of diff, please do so and contribute it.

Retrieved from "http://wiki.netbsd.se/diff"
Personal tools