← 返回命令列表

Linux command

cmp 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Compare two files

cmp [file1] [file2]

Silent comparison (exit code only)

cmp -s [file1] [file2]

Show all differences

cmp -l [file1] [file2]

Print differing bytes

cmp -b [file1] [file2]

Compare first N bytes

cmp -n [1024] [file1] [file2]

Skip bytes at start

cmp -i [100] [file1] [file2]

说明

cmp compares two files byte by byte and reports the location of the first difference. By default, it prints the byte number and line number where the files diverge, or produces no output if they are identical. Unlike diff, which operates on text lines, cmp works at the byte level, making it more efficient and appropriate for binary files. The -s (silent) mode is commonly used in scripts where only the exit status matters: 0 for identical files, 1 for differences, and 2 for errors. The -l option lists all byte positions where the files differ, along with the differing byte values in octal. The tool can also skip initial bytes in one or both files and limit the comparison to a specified number of bytes.

参数

-b, --print-bytes
Print differing bytes
-i _skip_, --ignore-initial _skip_
Skip first bytes of both files
-i _skip1:skip2_
Skip different amounts per file
-l, --verbose
Output all byte differences
-n _limit_, --bytes _limit_
Compare at most limit bytes
-s, --quiet, --silent
Suppress output, exit code only
--help
Display help
-v, --version
Show version

FAQ

What is the cmp command used for?

cmp compares two files byte by byte and reports the location of the first difference. By default, it prints the byte number and line number where the files diverge, or produces no output if they are identical. Unlike diff, which operates on text lines, cmp works at the byte level, making it more efficient and appropriate for binary files. The -s (silent) mode is commonly used in scripts where only the exit status matters: 0 for identical files, 1 for differences, and 2 for errors. The -l option lists all byte positions where the files differ, along with the differing byte values in octal. The tool can also skip initial bytes in one or both files and limit the comparison to a specified number of bytes.

How do I run a basic cmp example?

Run `cmp [file1] [file2]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -b, --print-bytes do in cmp?

Print differing bytes