一天一个shell命令 linux文本操作系列-diff命令详解
这又是一个文件比较命令,熟练掌握会节省你很多工作。还记得comm命令吗?diff比他更加复杂,好用,不仅能比较文件,还能比较目录。当一个文件有多个版本时候,或者更多复杂的文件,目录比较时,你会期望有一个比comm更有用的命令,diff正是为此而生。
全称:differentfile
实例:
文件1:v1.txt
hello worldv1_echo v1_diff v1_comm shelliseasy!
文件2:v2.txt
helloworld! v2_echo v1_diff easyshell!
1.普通比较:
$diffv1.txtv2.txt
1,2c1,2 <helloworld <v1_echo --- >helloworld! >v2_echo 4c4 <v1_comm --- >easyshell!
列出比较的区别,数字为行数,c是compare
2.一体化输出:
$diff-uv1.txtv2.txt>version.patch
-v1_echo +helloworld! +v2_echo v1_diff -v1_comm +easyshell! shelliseasy!
"+"表示补充的行,"-"表示删去的行
3.更新覆盖v1.txt,注意这里借用了patch
$patch–p1v1.txt<version.patch
4.比较v1,v2文件,仅输出v2的变化
$diff-ev1.txtv2.txt
4c easyshell! . 1,2c helloworld! v2_echo .
5.比较目录
借用书上一段解释:
-N将所有确实的文件视为空文件
-a将所有文件市委文件文本
-u生成一体化输出
-r遍历目录下的所有文件
比较两个目录下所有文本信息
$diff–Naurdirectory1directory2
除非你确定这两个目录基本相同,不然不推荐这么用。
6.工作疲劳后,尤其同时做很多事情的时候,会忘了有没有做过上传,或者修改某个目录下的文件。可以这样:
$diffv1.txt/${dir}
直接比较同名文件,高强度下的工作者相信真有体会。
解读下—help
太长,截取少部分解读一下。
Mandatoryargumentstolongoptionsaremandatoryforshortoptionstoo. --normal outputanormaldiff(thedefault) -q,--brief reportonlywhenfilesdiffer -s,--report-identical-files reportwhentwofilesarethesame -c,-CNUM,--context[=NUM] outputNUM(default3)linesofcopiedcontext -u,-UNUM,--unified[=NUM] outputNUM(default3)linesofunifiedcontext -e,--ed outputanedscript -n,--rcs outputanRCSformatdiff -y,--side-by-side outputintwocolumns FILES的格式可以是‘FILE1FILE2'、‘DIR1DIR2'、‘DIRFILE...'或 ‘FILE...DIR'。
解读:
1.-q–s
-q:只有两个不同是,提示“两个文件不同”
-s:两个文件相同时,提示“两个文件相同”
$diff-qsv1.txtv2.txt
(有点无聊的命令)
2.–y
按照列的方式输出,增加了直观性
helloworld |helloworld! v1_echo |v2_echo v1_diff v1_diff v1_comm |easyshell! shelliseasy! shelliseasy!
3.file后面的格式
可以是1.文件文件2.目录与目录3.文件与目录4.目录与文件
解释一下,文件与目录的形式,比较的是该目录下与比较文件同名的文件比较
如:$diffv1.txtdiff/
则实际是v1.txt与diff/v1.txt的比较。这是个很常用的功能。
对比comm
就比较文件而言,comm功能比较简单,但是也很直观,方便
而diff提供了更强大,更复杂的比较方式,还能对比目录,遍历目录。
用法:diff[选项]...FILES
CompareFILESlinebyline.
Mandatoryargumentstolongoptionsaremandatoryforshortoptionstoo.
--normal outputanormaldiff(thedefault)
-q,--brief reportonlywhenfilesdiffer
-s,--report-identical-files reportwhentwofilesarethesame
-c,-CNUM,--context[=NUM] outputNUM(default3)linesofcopiedcontext
-u,-UNUM,--unified[=NUM] outputNUM(default3)linesofunifiedcontext
-e,--ed outputanedscript
-n,--rcs outputanRCSformatdiff
-y,--side-by-side outputintwocolumns
-W,--width=NUM outputatmostNUM(default130)printcolumns
--left-column outputonlytheleftcolumnofcommonlines
--suppress-common-lines donotoutputcommonlines
-p,--show-c-function showwhichCfunctioneachchangeisin
-F,--show-function-line=RE showthemostrecentlinematchingRE
--labelLABEL useLABELinsteadoffilename
(canberepeated)
-t,--expand-tabs expandtabstospacesinoutput
-T,--initial-tab maketabslineupbyprependingatab
--tabsize=NUM tabstopseveryNUM(default8)printcolumns
--suppress-blank-empty suppressspaceortabbeforeemptyoutputlines
-l,--paginate passoutputthrough`pr'topaginateit
-r,--recursive recursivelycompareanysubdirectoriesfound
-N,--new-file treatabsentfilesasempty
--unidirectional-new-file treatabsentfirstfilesasempty
--ignore-file-name-case ignorecasewhencomparingfilenames
--no-ignore-file-name-case considercasewhencomparingfilenames
-x,--exclude=PAT excludefilesthatmatchPAT
-X,--exclude-from=FILE excludefilesthatmatchanypatterninFILE
-S,--starting-file=FILE startwithFILEwhencomparingdirectories
--from-file=FILE1 compareFILE1toalloperands;
FILE1canbeadirectory
--to-file=FILE2 comparealloperandstoFILE2;
FILE2canbeadirectory
-i,--ignore-case ignorecasedifferencesinfilecontents
-E,--ignore-tab-expansion ignorechangesduetotabexpansion
-Z,--ignore-trailing-space ignorewhitespaceatlineend
-b,--ignore-space-change ignorechangesintheamountofwhitespace
-w,--ignore-all-space ignoreallwhitespace
-B,--ignore-blank-lines ignorechangeswhoselinesareallblank
-I,--ignore-matching-lines=RE ignorechangeswhoselinesallmatchRE
-a,--text treatallfilesastext
--strip-trailing-cr striptrailingcarriagereturnoninput
-D,--ifdef=NAME outputmergedfilewith`#ifdefNAME'diffs
--GTYPE-group-format=GFMT formatGTYPEinputgroupswithGFMT
--line-format=LFMT formatallinputlineswithLFMT
--LTYPE-line-format=LFMT formatLTYPEinputlineswithLFMT
Theseformatoptionsprovidefine-grainedcontrolovertheoutput
ofdiff,generalizing-D/--ifdef.
LTYPE可以是‘old'、‘new'或‘unchanged'。GTYPE可以是LTYPE的选择
或是‘changed'。
GFMT(only)maycontain:
%< linesfromFILE1
%> linesfromFILE2
%= linescommontoFILE1andFILE2
%[-][WIDTH][.[PREC]]{doxX}LETTER printf-stylespecforLETTER
LETTERsareasfollowsfornewgroup,lowercaseforoldgroup:
F firstlinenumber
L lastlinenumber
N numberoflines=L-F+1
E F-1
M L+1
%(A=B?T:E) ifAequalsBthenTelseE
LFMT(only)maycontain:
%L contentsofline
%l contentsofline,excludinganytrailingnewline
%[-][WIDTH][.[PREC]]{doxX}n printf-stylespecforinputlinenumber
BothGFMTandLFMTmaycontain:
%% %
%c'C' thesinglecharacterC
%c'\OOO' thecharacterwithoctalcodeOOO
C thecharacterC(othercharactersrepresentthemselves)
-d,--minimal tryhardtofindasmallersetofchanges
--horizon-lines=NUM keepNUMlinesofthecommonprefixandsuffix
--speed-large-files assumelargefilesandmanyscatteredsmallchanges
--help displaythishelpandexit
-v,--version outputversioninformationandexit
FILES的格式可以是‘FILE1FILE2'、‘DIR1DIR2'、‘DIRFILE...'或
‘FILE...DIR'。
If--from-fileor--to-fileisgiven,therearenorestrictionsonFILE(s).
如果FILE是‘-',则由标准输入读取内容。
如果输入相同,则退出状态为0;1表示输入不同;2表示有错误产生。