Diff notes
📃 These are primarily notes, intended to be a collection of useful fragments, that will probably never be complete in any sense. |
(See also comm)
Things called 'diff' group roughly two types of problems:
- sequence alignment, such as trying to figure out differences in two versions of text or code
- set matching, such as trying to find the difference in two directories
These are close in that they maximize the amount of matches, but the first has an ordering constraint that the second does not.
diff
diff is an utility to show the (minimal line-based) difference between two text files.
In informal use it is useful to see where minor changes between files are.
Many prefer 'unified diff format' (-u parameter) over the default output format.
Its output can also be used by patch, to change another copy of the original file. This is used between programmers to distribute minor changes in code. In this context, it is quite helpful that diff lists its context, so more than one (non-conflicting) patch may be applied to the same file.
- git diff
- Yes, will work without any git metadata.
- Will color its output (if git is configured to).
- sdiff
- allows interactive merging
- colordiff
- wrapper around diff that colors the output.
- vimdiff
- n-way?(verify)
- emacs's ediff-buffers
3-version comparison
For example to show two different changes to an original version, which can be useful for conflict resolution in versioning systems.
binary diff
Often to do binary patches (e.g. to save on data transfer, which is now less pressing), but also sometimes to see before/after versions of a file that got changed, e.g. in a hex-editor sort of viewer.
GUIs
- Meld [1]
- Kompare (for KDE) [2]
- P4merge [3]
- Beyond Compare [4]
3-way:
- kdiff3 [5]
Difference in directory structure
...as in, check only the presence of files.
The shortest trick is probably:
rsync -navi dir1/ dir2/
Note:
- make sure -n is in there, or it will alter dir2 to match dir1
- the slashes matter (on the first, technically)
- This will use rsync's selection logic - i.e. is-same-timestamp-and-size.
- You may sometimes want --size-only, to look only at size and make mtime irrelevant.
- -i helps show how it's different
With content checks
The simplest graphical way is to use meld.
You could use diff itself - recursive, and ask it to just mention that files differ in contents:
diff -q -r dir1 dir2
Most other methods require more shell-fu, e.g.:
# diff -u 0 <( tree dir1 ) <( tree dir2 )
Or the three commands and temporary files:
tree dir1 > dir1tree tree dir2 > dir2tree diff dir1tree dir2tree