1. 程式人生 > >N天學習一個linux命令之diff

N天學習一個linux命令之diff

-name brief repo fix show 過濾 har 所在 XP

用途

按行比較文件差異,也可以比較目錄

用法

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

N天學習一個linux命令之diff