一天一个shell命令 linux文本操作系列-diff命令详解

2019-09-23 09:20:23丽君

 -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

FILES 的格式可以是‘FILE1 FILE2'、‘DIR1 DIR2'、‘DIR FILE...'或
‘FILE... DIR'。

解读:

1.-q –s

-q: 只有两个不同是,提示“两个文件不同”

-s: 两个文件相同时,提示“两个文件相同”

$diff -qs v1.txt v2.txt

(有点无聊的命令 )

2. –y

按照列的方式输出,增加了直观性


 hello world               | hello world!
 v1_echo                    | v2_echo
 v1_diff                       v1_diff
 v1_comm                   | easy shell!
 shell is easy!               shell is easy!

3. file 后面的格式

可以是1.文件 文件 2.目录与目录 3.文件与目录 4. 目录与文件
解释一下,文件与目录的形式,比较的是该目录下 与 比较文件同名的文件比较
如:$ diff v1.txt diff/
则实际是v1.txt 与 diff/v1.txt的比较。这是个很常用的功能。

对比comm
就比较文件而言,comm功能比较简单,但是也很直观,方便
而diff 提供了更强大,更复杂的比较方式,还能对比目录,遍历目录。


用法: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)