欢迎转载,转载请标明出处:
在GNU Linux系统编程中,经常会用到diff与patch生成补丁与打补丁。使用diff生成补丁,diff是Linux下的文件比较命令,参数这里就不说了,直接man一下就行了,不仅可以比较文件,也可以比较两个目录,并且可以将不同之处生成补丁文件,其实就是一种打补丁的命令。使用方法如下:
diff -rNu a b > diff.patch
其中,a是旧的目录或文件,b为最近修改后的目录或文件,生成的补丁文件为patch。
使用patch打补丁,patch命令的参数详见man手册,使用patch给另外一个a目录打补丁时如下:
patch -p0 < diff.patch
工具的官方说明如下:
You can usethe diff command to show differences between two files, or each correspondingfile in two directories. diff outputs differences between files line by line inany of several formats, selectable by command line options. This set ofdifferences is often called a `diff' or `patch'. For files that are identical,diff normally produces no output; for binary (non-text) files, diff normally reports only that theyare different.
You can usethe cmp command to show the offsets and line numbers where two files differ.cmp can also show all the characters that differ between the two files, side byside.
You can usethe diff3 command to show differences among three files. When two people havemade independent changes to a common original, diff3 can report the differencesbetween the original and the two changed versions, and can produce a mergedfile that contains both persons' changes together with warnings aboutconflicts.
You can usethe sdiff command to merge two files interactively.
http://gnuwin32.sourceforge.net/packages/diffutils.htm
包名:diffutils-2.8.7-1.exe
双击后,默认安装即可。
执行: diff –help
Usage: diff [OPTION]... FILES
Compare files line by line.
-i --ignore-case Ignore case differences in file contents.
--ignore-file-name-case Ignorecase when comparing file names.
--no-ignore-file-name-case Consider case when comparing file names.
-E --ignore-tab-expansion Ignore changes due to tab expansion.
-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 whose lines are all blank.
-IRE --ignore-matching-lines=RE Ignore changes whose lines all match RE.
--strip-trailing-cr Striptrailing carriage return on input.
--binary Read and write data inbinary mode.
-a --text Treat all files as text.
-c -C NUM --context[=NUM] Output NUM (default 3) lines of copiedcontext.
-u -U NUM --unified[=NUM] Output NUM (default 3) lines of unifiedcontext.
--label LABEL Use LABEL insteadof file name.
-p --show-c-function Show which C function each change is in.
-F RE --show-function-line=RE Show themost recent line matching RE.
-q --brief Output only whether files differ.
-e --ed Output an ed script.
--normal Output a normal diff.
-n --rcs Output an RCS format diff.
-y --side-by-side Output in two columns.
-W NUM --width=NUM Output at most NUM (default 130) printcolumns.
--left-column Output only theleft column of common lines.
--suppress-common-lines Do notoutput common lines.
-DNAME --ifdef=NAME Output merged file to show `#ifdef NAME'diffs.
--GTYPE-group-format=GFMT Similar, but format GTYPE input groups with GFMT.
--line-format=LFMT Similar, butformat all input lines with LFMT.
--LTYPE-line-format=LFMT Similar,but format LTYPE input lines with LFMT.
LTYPE is `old', `new', or `unchanged'. GTYPE is LTYPE or `changed'.
GFMT may contain:
%< lines from FILE1
%> lines from FILE2
%= lines common to FILE1 andFILE2
%[-][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
LFMT may contain:
%L contents of line
%l contents of line, excludingany trailing newline
%[-][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number
Either GFMT or LFMT may contain:
%% %
%c'C' the single character C
%c'\OOO' the character with octalcode OOO
-l --paginate Pass the output through `pr' to paginate it.
-t --expand-tabs Expand tabs to spaces in output.
-T --initial-tab Make tabs line up by prepending a tab.
--tabsize=NUM Tab stops are everyNUM (default 8) print columns.
-r --recursive Recursively compare any subdirectories found.
-N --new-file Treat absent files as empty.
--unidirectional-new-file Treatabsent first files as empty.
-s --report-identical-files Report when two files are the same.
-xPAT --exclude=PAT Exclude files that match PAT.
-XFILE --exclude-from=FILE Exclude files that match any pattern in FILE.
-SFILE --starting-file=FILE Start with FILE when comparing directories.
--from-file=FILE1 Compare FILE1to all operands. FILE1 can be adirectory.
--to-file=FILE2 Compare alloperands to FILE2. FILE2 can be adirectory.
--horizon-lines=NUM Keep NUMlines of the common prefix and suffix.
-d --minimal Try hard to find a smaller set of changes.
--speed-large-files Assume largefiles and many scattered small changes.
-v --version Output version info.
--help Output this help.
FILES are `FILE1 FILE2' or `DIR1 DIR2' or`DIR FILE...' or `FILE... DIR'.
If --from-file or --to-file is given, thereare no restrictions on FILES.
If a FILE is `-', read standard input.
Exit status is 0 if inputs are the same, 1if different, 2 if trouble.
Report bugs to<[email protected]>.