linuxcmp命令介绍和使用案例

韵味老鸟 2024-09-09 13:40:37

linux cmp 命令介绍和使用案例

cmp 是一个用于比较两个文件的命令行工具,主要用于逐字节比较,能够快速确定文件之间的差异。它在 Linux 和 Unix 系统中广泛使用,尤其适合检查二进制文件或文本文件的相似性

功能逐字节比较:cmp 命令逐字节比较两个文件,并在发现差异时输出第一个不同的字节和行号。简洁输出:如果两个文件相同,则不输出任何内容;如果不同,则输出差异位置。多种选项:提供多种选项以控制比较方式和输出格式。语法bashcmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]OPTION:可选参数,用于修改 cmp 的行为。FILE1 和 FILE2:要比较的两个文件。SKIP1 和 SKIP2:可选参数,表示在比较前跳过的字节数。

常用选项-b 或 --print-bytes:打印不同的字节。-l 或 --verbose:输出字节编号和不同字节的值。-s 或 --quiet:不输出任何内容,仅返回退出状态。-i N:跳过输入文件的前 N 个字节。-n N:限制比较的字节数为 N。

命令:

root@meng:~# which cmp

/usr/bin/cmp

root@meng:~# cmp

cmp: missing operand after 'cmp'

cmp: Try 'cmp --help' for more information.

root@meng:~# cpm --help

Command 'cpm' not found, but can be installed with:

apt install cpm

root@meng:~# cmp --help

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

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 suffixes:

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.

Report bugs to: bug-diffutils@gnu.org

GNU diffutils home page: <https://www.gnu.org/software/diffutils/>

General help using GNU software: <https://www.gnu.org/gethelp/>

案例:

root@meng:~# ls

f1.txt.bz2 f2.txt.bz2 meng.txt meng.txt.bz2 rec00001f1.txt snap tmp

root@meng:~# cmp f1.txt.bz2 f2.txt.bz2

f1.txt.bz2 f2.txt.bz2 differ: byte 11, line 1

root@meng:~# cmp f1.txt.bz2 rec00001f1.txt

f1.txt.bz2 rec00001f1.txt differ: byte 1, line 1

root@meng:~# cmp -b f1.txt.bz2 rec00001f1.txt

f1.txt.bz2 rec00001f1.txt differ: byte 1, line 1 is 102 B 146 f

root@meng:~# cmp -bf1.txt.bz2 f2.txt.bz2

cmp: invalid option -- 'f'

cmp: Try 'cmp --help' for more information.

root@meng:~# cmp -b f1.txt.bz2 f2.txt.bz2

f1.txt.bz2 f2.txt.bz2 differ: byte 11, line 1 is 314 M-L 276 M->

root@meng:~#

0 阅读:0

韵味老鸟

简介:感谢大家的关注