|
|
# **diff**
|
|
|
|
|
|
## 说明
|
|
|
|
|
|
**diff命令** 在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入
|
|
|
diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,则将会比较该目录中具有相同文件名的文件,而不
|
|
|
会对其子目录文件进行任何比较操作
|
|
|
|
|
|
## 选项
|
|
|
|
|
|
```markdown
|
|
|
|
|
|
-<行数>:指定要显示多少行的文本。此参数必须与-c或-u参数一并使用
|
|
|
-a或——text:diff预设只会逐行比较文本文件
|
|
|
-b或--ignore-space-change:不检查空格字符的不同
|
|
|
-B或--ignore-blank-lines:不检查空白行
|
|
|
-c:显示全部内容,并标出不同之处
|
|
|
-C<行数>或--context<行数>:与执行“-c-<行数>”指令相同
|
|
|
-d或——minimal:使用不同的演算法,以小的单位来做比较
|
|
|
-D<巨集名称>或ifdef<巨集名称>:此参数的输出格式可用于前置处理器巨集
|
|
|
-e或——ed:此参数的输出格式可用于ed的script文件
|
|
|
-f或-forward-ed:输出的格式类似ed的script文件,但按照原来文件的顺序来显示不同处
|
|
|
-H或--speed-large-files:比较大文件时,可加快速度
|
|
|
-l<字符或字符串>或--ignore-matching-lines<字符或字符串>:若两个文件在某几行有所不同,而之际航同时都包含了选项中指定的字符或字符串,则不显示这两个文件的差异
|
|
|
-i或--ignore-case:不检查大小写的不同
|
|
|
-l或——paginate:将结果交由pr程序来分页
|
|
|
-n或——rcs:将比较结果以RCS的格式来显示
|
|
|
-N或--new-file:在比较目录时,若文件A仅出现在某个目录中,预设会显示:Only in目录,文件A 若使用-N参数,则diff会将文件A 与一个空白的文件比较
|
|
|
-p:若比较的文件为C语言的程序码文件时,显示差异所在的函数名称
|
|
|
-P或--unidirectional-new-file:与-N类似,但只有当第二个目录包含了第一个目录所没有的文件时,才会将这个文件与空白的文件做比较
|
|
|
-q或--brief:仅显示有无差异,不显示详细的信息
|
|
|
-r或——recursive:比较子目录中的文件
|
|
|
-s或--report-identical-files:若没有发现任何差异,仍然显示信息
|
|
|
-S<文件>或--starting-file<文件>:在比较目录时,从指定的文件开始比较
|
|
|
-t或--expand-tabs:在输出时,将tab字符展开
|
|
|
-T或--initial-tab:在每行前面加上tab字符以便对齐
|
|
|
-u,-U<列数>或--unified=<列数>:以合并的方式来显示文件内容的不同
|
|
|
-v或——version:显示版本信息
|
|
|
-w或--ignore-all-space:忽略全部的空格字符
|
|
|
-W<宽度>或--width<宽度>:在使用-y参数时,指定栏宽
|
|
|
-x<文件名或目录>或--exclude<文件名或目录>:不比较选项中所指定的文件或目录
|
|
|
-X<文件>或--exclude-from<文件>;您可以将文件或目录类型存成文本文件,然后在=<文件>中指定此文本文件
|
|
|
-y或--side-by-side:以并列的方式显示文件的异同之处
|
|
|
--help:显示帮助
|
|
|
--left-column:在使用-y参数时,若两个文件某一行内容相同,则仅在左侧的栏位显示该行内容
|
|
|
--suppress-common-lines:在使用-y参数时,仅显示不同之处
|
|
|
|
|
|
用法:diff [选项]... FILES
|
|
|
Compare FILES line by line.
|
|
|
|
|
|
Mandatory arguments to long options are mandatory for short options too.
|
|
|
--normal output a normal diff (the default)
|
|
|
-q, --brief report only when files differ
|
|
|
-s, --report-identical-files report when two files are the same
|
|
|
-c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context
|
|
|
-u, -U NUM, --unified[=NUM] output NUM (default 3) lines of unified context
|
|
|
-e, --ed output an ed script
|
|
|
-n, --rcs output an RCS format diff
|
|
|
-y, --side-by-side output in two columns
|
|
|
-W, --width=NUM output at most NUM (default 130) print columns
|
|
|
--left-column output only the left column of common lines
|
|
|
--suppress-common-lines do not output common lines
|
|
|
|
|
|
-p, --show-c-function show which C function each change is in
|
|
|
-F, --show-function-line=RE show the most recent line matching RE
|
|
|
--label LABEL use LABEL instead of file name
|
|
|
(can be repeated)
|
|
|
|
|
|
-t, --expand-tabs expand tabs to spaces in output
|
|
|
-T, --initial-tab make tabs line up by prepending a tab
|
|
|
--tabsize=NUM tab stops every NUM (default 8) print columns
|
|
|
--suppress-blank-empty suppress space or tab before empty output lines
|
|
|
-l, --paginate pass output through 'pr' to paginate it
|
|
|
|
|
|
-r, --recursive recursively compare any subdirectories found
|
|
|
--no-dereference don't follow symbolic links
|
|
|
-N, --new-file treat absent files as empty
|
|
|
--unidirectional-new-file treat absent first files as empty
|
|
|
--ignore-file-name-case ignore case when comparing file names
|
|
|
--no-ignore-file-name-case consider case when comparing file names
|
|
|
-x, --exclude=PAT exclude files that match PAT
|
|
|
-X, --exclude-from=FILE exclude files that match any pattern in FILE
|
|
|
-S, --starting-file=FILE start with FILE when comparing directories
|
|
|
--from-file=FILE1 compare FILE1 to all operands;
|
|
|
FILE1 can be a directory
|
|
|
--to-file=FILE2 compare all operands to FILE2;
|
|
|
FILE2 can be a directory
|
|
|
|
|
|
-i, --ignore-case ignore case differences in file contents
|
|
|
-E, --ignore-tab-expansion ignore changes due to tab expansion
|
|
|
-Z, --ignore-trailing-space ignore white space at line end
|
|
|
-b, --ignore-space-change ignore changes in the amount of white space
|
|
|
-w, --ignore-all-space ignore all white space
|
|
|
-B, --ignore-blank-lines ignore changes where lines are all blank
|
|
|
-I, --ignore-matching-lines=RE ignore changes where all lines match RE
|
|
|
|
|
|
-a, --text treat all files as text
|
|
|
--strip-trailing-cr strip trailing carriage return on input
|
|
|
|
|
|
-D, --ifdef=NAME output merged file with '#ifdef NAME' diffs
|
|
|
--GTYPE-group-format=GFMT format GTYPE input groups with GFMT
|
|
|
--line-format=LFMT format all input lines with LFMT
|
|
|
--LTYPE-line-format=LFMT format LTYPE input lines with LFMT
|
|
|
These format options provide fine-grained control over the output
|
|
|
of diff, generalizing -D/--ifdef.
|
|
|
LTYPE is 'old', 'new', or 'unchanged'. GTYPE is LTYPE or 'changed'.
|
|
|
GFMT (only) may contain:
|
|
|
%< lines from FILE1
|
|
|
%> lines from FILE2
|
|
|
%= lines common to FILE1 and FILE2
|
|
|
%[-][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
|
|
|
%(A=B?T:E) if A equals B then T else E
|
|
|
LFMT (only) may contain:
|
|
|
%L contents of line
|
|
|
%l contents of line, excluding any trailing newline
|
|
|
%[-][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number
|
|
|
Both GFMT and LFMT may contain:
|
|
|
%% %
|
|
|
%c'C' the single character C
|
|
|
%c'\OOO' the character with octal code OOO
|
|
|
C the character C (other characters represent themselves)
|
|
|
|
|
|
-d, --minimal try hard to find a smaller set of changes
|
|
|
--horizon-lines=NUM keep NUM lines of the common prefix and suffix
|
|
|
--speed-large-files assume large files and many scattered small changes
|
|
|
|
|
|
--help display this help and exit
|
|
|
-v, --version output version information and exit
|
|
|
|
|
|
FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE...' or 'FILE... DIR'.
|
|
|
If --from-file or --to-file is given, there are no restrictions on FILE(s).
|
|
|
If a FILE is '-', read standard input.
|
|
|
如果输入相同,则退出状态为 0;1 表示输入不同;2 表示有错误产生
|
|
|
|
|
|
```
|
|
|
|
|
|
## 实例
|
|
|
|
|
|
```bash
|
|
|
diff /usr/li/test.txt test.txt # 将目录`/usr/li`下的文件test.txt与当前目录下的文件test.txt进行比较
|
|
|
|
|
|
: << comment
|
|
|
输出详解:
|
|
|
n1 a n3,n4
|
|
|
n1,n2 d n3
|
|
|
n1,n2 c n3,n4
|
|
|
|
|
|
其中,字母"a"、"d"、"c"分别表示添加、删除及修改操作。而"n1"、"n2"表示在文件1中的行号,"n3"、"n4"表示在文件2中的行号
|
|
|
注意:以上说明指定了两个文件中不同处的行号及其相应的操作。在输出形式中,每一行后面将跟随受到影响的若干行。其中,以<开始
|
|
|
的行属于文件1,以>开始的行属于文件2
|
|
|
comment
|
|
|
|
|
|
```
|
|
|
|