每天一个Linux 命令(7) cmp

cmp compare
cmp命令用来比较两个文件是否有差异。当相互比较的两个文件完全一样时,则该指令不会显示任何信息。若发现有差异,预设会标示出第一个不通之处的字符和列数编号。若不指定任何文件名称或是所给予的文件名为“-”,则cmp指令会从标准输入设备读取数据。
语法
cmp(选项)(参数)
选项
-c或--print-chars:除了标明差异处的十进制字码之外,一并显示该字符所对应字符;
-i<字符数目>或--ignore-initial=<字符数目>:指定一个数目;
-l或——verbose:标示出所有不一样的地方;
-s或--quiet或——silent:不显示错误信息;
-v或——version:显示版本信息;
--help:在线帮助。
参数
目录:比较两个文件的差异。
实例
使用cmp命令比较文件"testfile"和文件"testfile1"两个文件,则输入下面的命令:

cmp testfile testfile1 #比较两个指定的文件

cat testfile                    #查看文件内容  
Absncn 50                       #显示文件“testfile”  
Asldssja 60  
Jslkadjls 85 

cat testfile1                   #查看文件内容  
Absncn 50                       #显示文件“testfile1”  
AsldssjE 62  
Jslkadjls 85  

然后,再执行cmp命令,并返回比较结果,具体如下所示:

cmp testfile testfile1       #比较两个文件  
testfile testfile1           #有差异:第8字节,第2行  

注意:在比较结果中,只能够显示第一比较结果。

NAME

   cmp - compare two files byte by byte

SYNOPSIS

   cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

DESCRIPTION

   Compare two files byte by byte.

   The optional SKIP1 and SKIP2 specify the number of bytes to skip
   at the beginning of each file (zero by default).

   Mandatory arguments to long  options  are  mandatory  for  short
   options too.

   -b, --print-bytes
          print differing bytes

   -i, --ignore-initial=SKIP
          skip first SKIP bytes of both inputs

   -i, --ignore-initial=SKIP1:SKIP2
          skip  first SKIP1 bytes of FILE1 and first SKIP2 bytes of
          FILE2

   -l, --verbose
          output byte numbers and differing byte values

   -n, --bytes=LIMIT
          compare at most LIMIT bytes

   -s, --quiet, --silent
          suppress all normal output

   --help display this help and exit

   -v, --version
          output version information and exit

   SKIP values may be followed by the following multiplicative suf‐
   fixes:   kB   1000,  K  1024,  MB  1,000,000,  M  1,048,576,  GB
   1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.

   If a FILE is '-' or missing, read standard input.   Exit  status
   is 0 if inputs are the same, 1 if different, 2 if trouble.

AUTHOR

   Written by Torbjorn Granlund and David MacKenzie.

REPORTING BUGS

   Report bugs to: bug-diffutils@gnu.org
   GNU  diffutils  home  page:  <http://www.gnu.org/software/diffu‐
   tils/>
   General help using GNU software: <http://www.gnu.org/gethelp/>

COPYRIGHT

   Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+:
   GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
   This  is  free software: you are free to change and redistribute
   it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

   diff(1), diff3(1), sdiff(1)

   The full documentation for cmp is maintained as a  Texinfo  man‐
   ual.   If  the  info  and cmp programs are properly installed at
   your site, the command

          info cmp

   should give you access to the complete manual.

本文链接:

https://heyzen.club/index.php/Coder/89.html
1 + 2 =
快来做第一个评论的人吧~