diff

display differences between two files (or directories)

diff [options]   file1|-   file2|-
- specifies standard input.

diff directoryX file   
Compare directoryX/file with $PWD/file No need to specify $PWD.


diff directory directory
 Compares files in the directories, in alphabetical order.
Not recursive unless --recursive is given.

diff does not compares the actual contents of a directory as if it were a file.
The file that is fully specified may not be standard input, because standard input is nameless and the notion of "file with the same name" does not apply.

Use -- to terminate options when specifing file names which begin with a -.
Multiple single letter options can be combined. Example: ‑wyW50 (whiteSpaceIgnored, sidebYside, width=50)

comm show lines in common
cmp show bytes differing
sdiffSide-by-side merge of file differences.

Options to ignore "insignificant" differences.

If there are only ignored differences
file1 file2 are identical
is output and return code is 0.
-i
--ignore-case
 
-B
--ignore-blank-lines
Ignore differences that just insert or delete Blank lines
-b
-ignore-space-change
Ignore differences in amount of blanks
-w
-ignore-all-space
Ignore white space lines,
including ␍␊ (0D0A ) verses (0A alone) as in Apple Mac vs linux vs MSWindows.
-I regexp
--ignore-matching-lines= regexp
Ignore differences that just insert or delete lines that match regexp.
Option affecting amount of output
-q
--brief
quite, do not show differences, only whether the files differ.
Output "Files old-file and new-file differ" or nothing

Useful when comparing directories. See cmp for "quiter".

To check the return code $?

diff --brief … xxx xxx'
if [ $? -gt 0  ]; then echo -n  $'\a ' ;fi  #alert bell 
-t
--expand-tabs
to spaces in the output to preserve alignment.
-T
--initial-tab
Output a Tab rather than a space before the text of a line in normal or context format, causes the alignment to look normal.
--side-by-side
-y
  diff -y dfdf dfdf_1
Avail   Mounted                                                 Avail   Mounted
695M    /                                                       695M    /
170M    /Volumes/DATA                                           170M    /Volumes/DATA
56K     /Volumes/BACKUP_2                                       56K     /Volumes/BACKUP_2
919M    /Volumes/TBD                                          | 898M    /Volumes/TBD
xxxx                                                            xxxx
-W columns
--width=columns
diff -W70 --side-by-side dfdf dfdf_1
Avail   Mounted                         Avail   Mounted
695M    /                               695M    /
170M    /Volumes/DATA                   170M    /Volumes/DATA
56K     /Volumes/BACKUP_2               56K     /Volumes/BACKUP_2
919M    /Volumes/TBD              |     898M    /Volumes/TBD
xxxx                                    xxxx
--suppress-common-lines
diff  -wyW$COLUMNS --su    dfdf dfdf_1
diff -yW50 --suppress dfdf dfdf_1 
919M    /Volumes/TBD                                         | 898M    /Volumes/TBD
--leftcolumn only
diff -yW60 --left dfdf dfdf_1
Avail   Mounted              (
695M    /                    (
170M    /Volumes/DATA        (
56K     /Volumes/BACKUP_2    (
919M    /Volumes/TBD         |  898M    /Volumes/TBD
xxxx                         ( 

--horizon-lines=lines Do not discard the last lines lines of the common prefix and the first lines lines of the common suffix.
--from-file= file Compare file to all operands. file can be a directory.
--to-file= file Compare all operands to file. file can be a directory.
-l
--paginate
Pass the output through pr to paginate it.
-a
--text
Treat all files as ASCII text and compare them line-by-line, even if they do not seem to be text.
-v
--version
 

Show the differences with some lines before and after the differences, i.e. in context.

-c Use the context output format
-C lines
--context=lines. Show lines of context. Default 3.

diff  -c dfdf1 dfdf2
*** dfdf1 Tue Sep  5 18:10:15 2006  ← mod date of file differs, line is prefixed with stars 
--- dfdf2 Tue Sep  5 18:10:24 2006
***************  ← seperator
*** 4,7 **** ← position (column,line) where difference starts in dfdf1
  1.3G  /Volumes/PHOTOS_1
  2.7G  /Volumes/PHOTOS_2
  56K   /Volumes/BACKUP_2
! 919M  /Volumes/TBD
 ↖ position where difference starts
----4,7 ---- ← position (column,line) where difference starts in dfdf2
  1.3G  /Volumes/PHOTOS_1
  2.7G  /Volumes/PHOTOS_2
  56K   /Volumes/BACKUP_2
! 898M  /Volumes/TBD 
  -e
--ed Make output that is a valid ed script.
7c
898M    /Volumes/TBD
.

  -f
--forward-ed Make output that looks vaguely like an ed script but has changes in the order they appear in the file.

diff  -f dfdf dfdf_1
c7
898M    /Volumes/TBD
.

  -F regexp In context and unified format for each hunk of differences, show some of the last preceding line that matches regexp.

  -n
--rcs Output RCS-format(Revision Control Systems) diffs; like -f except that each command specifies the number of lines affected.

diff  -n dfdf dfdf_1
d7 1
a7 1
898M    /Volumes/TBD

  -L label
--label=label Use label instead of the file name in the context and unified format headers.

-D name
--ifdef=name Make merged if-then-else format output, conditional on the preprocessor macro name.

--line-format=format Use format to output all input lines in in-then-else format.

%L contents of line %l , excluding any trailing newline
%[-]*[width][.[prec*{doxX}*n printf-style spec for input line number
%% the % character
x the single character x (i.e. any character)
%c'\OOO' the character with octal code OOO
diff --line-format="%8dn  %L" dfdf dfdf_1
       1  Avail Mounted
       2  695M  /
       3  170M  /Volumes/DATA
       4  1.3G  /Volumes/PHOTOS_1
       5  2.7G  /Volumes/PHOTOS_2
       6  56K   /Volumes/BACKUP_2
       7  919M  /Volumes/TBD
       7  898M  /Volumes/TBD
       8  xxxx
--old-line-format=format... a line taken from just the first file
--new-line-format=format ...a line taken from just the second file
--unchanged-line-format=format ... a line common to both files

% lines from FILE1
%> lines from FILE2
%= lines common to FILE1 and FILE2
%% %
%c'C' the single character C
%c'\OOO' the character with octal code OOO
%[-][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER
LETTERs are as follows for new group, lower case for old group:
F first line number
L last line number
N number of lines = L-F+1
E F-1
M L+1
--changed-group-format=format Use format to output a line group containing differing lines from both files
--old-group-format=format ... the first file
--new-group-format=format ...the second
--unchanged-group-format=format ... of common lines from both files

-u Use the unified output format.
-U lines
--unified[=lines]
Use the unified output format, showing lines of context. default 3.
#cat 0

Avail   Mounted
10632   /
791     /Volumes/DATA
560     /Volumes/PHOTOS
3504    /Volumes/BACKUPS
4542    /Volumes/linux
934     /Volumes/SWAP
#cat 00

Avail   Mounted
10639   /
791     /Volumes/DATA
560     /Volumes/PHOTOS
3504    /Volumes/BACKUPS
4542    /Volumes/linux
934     /Volumes/SWAP

diff -u 0 00
--- 0   2007-01-14 21:07:11.000000000 -0500
+++ 00  2007-01-14 21:15:54.000000000 -0500
@@ -1,5 +1,5 @@
 Avail  Mounted
-10632  /
+10639  /
 791    /Volumes/DATA
 560    /Volumes/PHOTOS
 3504   /Volumes/BACKUPS

-p
--show-c-function
--show-function-line=regexp In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp.

--sdiff-merge-assist Print extra information to help sdiff. sdiff uses this option when it runs diff. This option is not intended for users to use directly.

-d --minimal Change the algorithm to perhaps find a smaller set of changes. slower (sometimes much slower).
-H
--speed-large-files Use Heuristics to speed handling of large files that have numerous scattered small changes.

When comparing directories:

-s
--report-identical-files
Report "are identical" when files are the same based on the data compared!
 otherwise no output is produced.
-q
--brief
quite, do not show differences, only whether the files differ.
Output "Files old-file and new-file differ" or nothing
-x pattern
--exclude=pattern
exclude files and subdirectories whose basenames match pattern.
--exclude-from=file exclude files and subdirectories whose basenames match any pattern in file.
-X file eXclude files and subdirectories whose basenames match any pattern contained in file.
-r
--recursive
recursively compare subdirectories .
-N
--new-file
a file found in only in one directory is compared to an empty file.
-P
--unidirectional-new-file
a file occuring only in the second directory is compared to an empty file.
-S file
--starting-file=file
start with file. Perhaps resuming an aborted comparison.

See

cmp , comm, diff3, ed, patch, pr, sdiff.

Diagnostics

Return Code
0: No differences were found based on the data compared! i
     file1 file2 are identical is output.
1: Differences were found
2: Binary file mismatch, file inaccessible,….



comm

Compare sorted files displaying lines in common.

comm [option]file1 file2

With no options, produce three-column output.
first Column contains lines unique to FILE1,
second column contains lines unique to FILE2 and
last column contains lines common to both files.

-1 suppress lines unique to FILE1 (first column)
-2 suppress lines unique to FILE2 (secondcolumn)
-3 suppress lines that appear in both files (last column
--help
--version
> cat 1
aaaaaaaaa
bbbbbbbb
ccccccc
> cat 2
aaaaaaaaa
b222bbbb
ccccccc
> comm 1 2
                  aaaaaaaaa
         b222bbbb
bbbbbbbb
                  ccccccc 

cmp

Show characters/bytes differing

cmp options file1 file2

cmp [option]… file1 [file2 [skip1 [skip2]]]

-b
--print-bytes
bytes which differ are output.
file1 file2 differ: byte 1232, line 38 is 
 164 t
     file1
163 s
 file2
-i ignore1[:ignore2]
--ignore-initial=ignore1
[:ignore2]
ignore the first ignore1 bytes of file1 and the first ignore2 bytes of file2.
-l
--verbose
list all differing bytes. Otherwise stops at first non compare.
byte
Number
decimal Octal Octal!
    11  127  153   
    12  213  222
    13   53  125
    14  124  125
    15   24  142
    16  115  247
…
-n number
--bytes=number
number of bytes to compare.
-s
--quiet
--silent
silently. Test the return code as shown in --brief
-v
--version
--help

Return values:
0 - files are identical in the section compared! No message is output.
1 - files differ.
2 - inaccessible or missing argumen.

Errors:
cmp: invalid --ignore-initial value are there more then 2 files listed? alternate format

 > cat 1  
aaa     aaaaaa  the space after aaa is due to a  (i.e. Horizontal Tab, x'09')
bbbbbbbb
ccccccc
> cat 2
aaa     aaaaaa
b222bbbb
ccccccc

>cmp 1 2
1 2 differ: char 13, line 2
  
char count includes tab as 1 and  
> hexdump -s 12 -n 5 -C a b  # skip 12 (i.e. start at 13) show 5
0000000c  62 62 62 62 62                                    |bbbbb|
00000011
> hexdump -s 12 -n 5 -C b  
0000000c  32 32 32 62 62                                    |222bb|
00000011
>cmp --verbose F8011/wstation.dat F8019/wstation.dat | column  -c140 -x  # column across
    -Octal-      -Octal-
 76 245  75  77   1  24  78 127 203  79  75 274  80 263 200  81  76  70  82 221 170  83 261 156
 84 323 337  85   6  23  86 347 324  87 164  34  88 154 247  89 147 217  90 131 111  91  37 334
 92 222  30  93 267 375  94  57 232  95 366 227  96 244 367  97   0  10  98 314  14  99 203 365
100 326 272 101 116 352 102 244  71 103  35  16 104 202 270 105  56 102 106 341 357 107 372 125
108 124 114 109 143 253 110 217  61 111 223 166 112   3 342 113 263 331 114 141 367 115 325 164
116 135 341 117  47 352 118 200  72 119  34 266 120 170 116 121 261 107 122   6  54 123  73  23
124  37 121 125 324 173 126 171 125 127 252   3 128  16  21 129 123 101 130  16 313 131 306 257
132 175  77 133  10 125 134  66 175 135 253 167 136 153   1 137 204  27 138 264 162 139 245  13



sdiff

side-by-side merge of file differences

sdiff [option]file1 file2

If one of filen is -, reads standard input.

    Ignore minor differences
-i
--ignore-case
 
-E
--ignore-tab-expansion
due to tab expansion.
-b
--ignore-space-change
amount of white space.
-W
--ignore-all-space
all white space.
-B
--ignore-blank-lines
are all blank.
-I RE
--ignore-matching-lines=RE
matching RE.
--strip-trailing-cr Strip trailing carriage return on input.
-a
--text
Treat all files as ASCII text.
   output controls
-w num
--width=num
at most num (default 130) columns per line.
-l
--left-column
only the left column of common lines.
-s
--suppress-common-lines
 
-t
--expand-tabs
Expand tabs to spaces in output.
   Performace
-d
--minimal
Try hard to find a smaller set of changes.
-H
--speed-large-files
Assume large files and many scattered small changes.
-o file
--output=file
output to file.
--diff-program=program Use program to compare files.
-v
--version

--help

bdiff { filename1 | - } { filename2 | - } [ number ] [ -s ] splits files into number pieces and the call diff

diff aka top