N天学习一个linux命令之diff

用途

按行比较文件差异,也可以比较目录

用法

diff [OPTION]... FILES

常用选项

-i --ignore-case
忽略大小写

--ignore-file-name-case
忽略文件名大小写

--no-ignore-file-name-case
文件名区分大小写

-E --ignore-tab-expansion
忽略tab字符

-b --ignore-space-change
忽略空格

-w --ignore-all-space
忽略所有空格

-B --ignore-blank-lines
忽略空白行

-I RE --ignore-matching-lines=RE
过滤匹配的行

--strip-trailing-cr
去掉行尾回车符

-a --text
所有文件内容当做文本

-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.)

--label LABEL
使用LABEL代替文件名

-p --show-c-function
显示差异行所在文件

-F RE --show-function-line=RE
(Show the most recent line matching RE.)

-q --brief
直接输出结果是否有差异

-e --ed
(Output an ed script.)

--normal
(Output a normal diff.)

-n --rcs
(Output an RCS format diff.)

-y --side-by-side
同时输出2个文件的内容,对照显示

-W NUM --width=NUM
(Output at most NUM (default 130) print columns.)

--left-column
左边输出完整内容,右边只输出差异

--suppress-common-lines
只输出差异行

-D NAME --ifdef=NAME
(Output merged file to show ‘#ifdef NAME’ diffs.)

--GTYPE-group-format=GFMT
(Similar, but format GTYPE input groups with GFMT.)
(GTYPE is LTYPE or ‘changed’.)

--line-format=LFMT
(Similar, but format all input lines with LFMT.)

--LTYPE-line-format=LFMT
(Similar, but format LTYPE input lines with LFMT.)
(LTYPE is ‘old’, ‘new’, or ‘unchanged’.)

-l --paginate
分页显示

-t --expand-tabs
输出时,tab字符换成空格

-T --initial-tab
(Make tabs line up by prepending a tab.)

-r --recursive
(Recursively compare any subdirectories found.)

-N --new-file
(Treat absent files as empty.)

--unidirectional-new-file
(Treat absent first files as empty.)

-s --report-identical-files
(Report when two files are the same.)

-x PAT --exclude=PAT
(Exclude files that match PAT.)

-X FILE --exclude-from=FILE
(Exclude files that match any pattern in FILE.)

-S FILE --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.)

--horizon-lines=NUM
(Keep NUM lines of the common prefix and suffix.)

-d --minimal
(Try hard to find a smaller set of changes.)

--speed-large-files
(Assume large files and many scattered small changes.)

-v --version
输出程序版本信息

--help
显示帮助信息

实践

1 比较2个文件

# 显示所有
diff -y dir1/friend.php friend.php

# 只显示差异
diff -y --suppress-common-lines dir1/friend.php friend.php

# 只显示左边完整内容,右边只显示差异内容
diff -y --left-column dir1/friend.php friend.php

擦参考资料

【1】man diff

原文地址:https://www.cnblogs.com/wadeyu/p/8885698.html