hexdump 命令详解与参数实测(Ubuntu 22.04)
hexdump 详解:-C 经典视图、-e 自定义格式、-n/-s 区段控制与 od 输出对照,Ubuntu 22.04 实测
hexdump 命令详解与参数实测(Ubuntu 22.04)
目标:全面掌握 hexdump 的每一个常用参数——
-C经典 hex+ASCII 视图、-b/-d/-o/-x四种数值视图、-e自定义格式、-n限长、-s跳过偏移,并给出与od -t x1z的输出对照,全部命令与回显已在 Ubuntu 22.04(util-linux 2.37.2)实测通过。
一、环境信息
本篇所有命令与回显均在以下环境实测采集,读者在同等版本环境可逐条复现。
- 操作系统:Ubuntu 22.04.5 LTS(x86_64)
- hexdump 版本:hexdump from util-linux 2.37.2(
hexdump -V实测),由 bsdextrautils 包提供(dpkg -S实测),Ubuntu 22.04 默认预装,无需额外安装 - 配套工具:GNU od(输出对照)
- 演示目录:
/root/demo/hexdump/ - 测试数据:
bin16.bin(16 字节二进制样例,内容为Hello, world!\n加0xFF 0x01)、text2.txt(3 字节文本AB\n)
二、命令概览
hexdump 的职责是把文件按十六进制、十进制、八进制或 ASCII 形式转储显示,其中 -C(canonical,规范格式)输出的「十六进制 + 竖线包裹的 ASCII 对照」是业界最经典的二进制肉眼检查视图。它与 od 同源(都源自 BSD),但选项更精简、-e 自定义格式语法更灵活。
💡 技术拓展:字节级查看工具中 hexdump 的定位——od 格式体系最全且是 POSIX 标准(见 od 篇技术拓展表),xxd 胜在能用
-r从 hex 还原二进制,hexdump 则以-C视图的易读性和-e格式的灵活性见长;快速肉眼检查二进制首选 hexdump -C,脚本化多格式解析首选 od。
工具 来源 代表能力 适用场景 od GNU coreutils -t十余种格式、POSIX 标准脚本化多格式解析 hexdump util-linux -C经典 hex+ASCII 视图、-e自定义格式快速肉眼检查(本文主角) xxd vim 附带 -r从 hex 文本还原二进制十六进制与二进制互转
2.1 语法与测试数据准备
hexdump 的语法为 hexdump [options] <file>...(源自 --help 原文)。准备贯穿全篇的 16 字节样例:
mkdir -p /root/demo/hexdump
cd /root/demo/hexdump
printf 'Hello, world!\n\xff\x01' > bin16.bin
printf 'AB\n' > text2.txt
ls -l bin16.bin text2.txt
数据就绪回显:
-rw-r--r-- 1 root root 16 Oct 15 07:42 bin16.bin
-rw-r--r-- 1 root root 3 Oct 15 07:42 text2.txt
2.2 --help 全量回显
hexdump --help 输出简短,完整保留:
Usage:
hexdump [options] <file>...
Display file contents in hexadecimal, decimal, octal, or ascii.
Options:
-b, --one-byte-octal one-byte octal display
-c, --one-byte-char one-byte character display
-C, --canonical canonical hex+ASCII display
-d, --two-bytes-decimal two-byte decimal display
-o, --two-bytes-octal two-byte octal display
-x, --two-bytes-hex two-byte hexadecimal display
-L, --color[=<mode>] interpret color formatting specifiers
colors are enabled by default
-e, --format <format> format string to be used for displaying data
-f, --format-file <file> file that contains format strings
-n, --length <length> interpret only length bytes of input
-s, --skip <offset> skip offset bytes from the beginning
-v, --no-squeezing output identical lines
-h, --help display this help
-V, --version display version
Arguments:
<length> and <offset> arguments may be followed by the suffixes for
GiB, TiB, PiB, EiB, ZiB, and YiB (the "iB" is optional)
For more details see hexdump(1).
2.3 版本确认与包归属
确认版本与提供该命令的软件包:
$ dpkg -S /usr/bin/hexdump
bsdextrautils: /usr/bin/hexdump
$ hexdump -V 2>&1 || true
hexdump from util-linux 2.37.2
说明:Ubuntu 22.04 的 hexdump 属于 bsdextrautils 包(util-linux 套件的一部分);hexdump -V 是版本输出选项(注意是 -V 大写,与 od 的 --version 不同)。
三、参数逐项实测
本章所有实测基于 2.1 节的 bin16.bin(16 字节),便于横向对比各视图。
3.1 -C(--canonical):经典十六进制 + ASCII 视图
$ hexdump -C bin16.bin
00000000 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01 |Hello, world!...|
00000010
$ hexdump -C text2.txt
00000000 41 42 0a |AB.|
00000003
说明:每行 16 字节,中间以双空格分成两组各 8 字节;行尾竖线内是 ASCII 对照(不可打印字符显示为 .);末行是文件总长(十六进制 00000010 = 16 字节、00000003 = 3 字节)。text2.txt 一行只有 3 字节,不足 16 的部分以空格填充。
3.2 -b(--one-byte-octal):单字节八进制
$ hexdump -b bin16.bin
0000000 110 145 154 154 157 054 040 167 157 162 154 144 041 012 377 001
0000010
说明:每个字节以 3 位八进制显示,110 即 0x48(字符 H)、012 即换行、377 即 0xFF。地址列为 7 位十六进制。
3.3 -d(--two-bytes-decimal):两字节无符号十进制
$ hexdump -d bin16.bin
0000000 25928 27756 11375 30496 29295 25708 02593 00511
0000010
说明:每两字节组成一个无符号十进制数(小端序),如 48 65 组为 0x6548 = 25928。
3.4 -o(--two-bytes-octal)与 -x(--two-bytes-hex)
-o 两字节八进制:
$ hexdump -o bin16.bin
0000000 062510 066154 026157 073440 071157 062154 005041 000777
0000010
-x 两字节十六进制(字节对按小端反转显示,内存 48 65 显示为 6548):
$ hexdump -x bin16.bin
0000000 6548 6c6c 2c6f 7720 726f 646c 0a21 01ff
0000010
💡 说明:
-b/-d/-o/-x与 od 的同名传统选项语义一致,两者输出数值相同、仅列宽排版略有差异;日常肉眼检查用-C,需要数值视图时 od 的-t体系更细(可选字节宽度)。
3.5 -e(--format):自定义显示格式
-e 接一段「重复次数/字节单位 "格式串" "行尾串"」的格式描述。第一个例子:每行 16 个单字节、按 %02x 两位十六进制加空格显示,行尾换行:
$ hexdump -e "16/1 \"%02x \" \"\n\"" bin16.bin
48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01
第二个例子:每行前 8 字节按 3 位八进制加竖线、后 8 字节按可打印字符显示,演示 -e 的多段组合能力:
$ hexdump -e "8/1 \"%03o \" \"|\"" -e "8/1 \"%_p\"" -e "\"\n\"" bin16.bin
110 145 154 154 157 054 040 167|Hello, w
157 162 154 144 041 012 377 001|orld!...
说明:%_p 把可打印字节显示为字符、其余显示为 .;多个 -e 段按顺序拼在同一行输出,最后一个段负责换行。复杂格式串也可放入文件用 -f 引用(见 2.2 节 help 原文)。
3.6 -n(--length):限制读取字节数
$ hexdump -C -n 8 bin16.bin
00000000 48 65 6c 6c 6f 2c 20 77 |Hello, w|
00000008
说明:只解释前 8 字节,末行地址相应为 00000008。-n 与 -s 组合可转储任意区段。
3.7 -s(--skip):跳过起始偏移
从偏移 4 开始转储到文件尾:
$ hexdump -C -s 4 bin16.bin
00000004 6f 2c 20 77 6f 72 6c 64 21 0a ff 01 |o, world!...|
00000010
从偏移 4 开始、只取 8 字节:
$ hexdump -C -s 4 -n 8 bin16.bin
00000004 6f 2c 20 77 6f 72 6c 64 |o, world|
0000000c
说明:地址列从实际偏移 00000004 起算;末行 0000000c 是十六进制 12(偏移 4 + 8 字节)。
3.8 与 od -t x1z 输出对照
同一个文件,hexdump -C 与 od -t x1z 的字节数值完全一致,排版不同:
$ od -t x1z bin16.bin
0000000 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01 >Hello, world!...<
0000020
差异一目了然:hexdump -C 地址为 8 位十六进制、每行固定 16 字节且分两组、ASCII 对照用竖线包裹;od -t x1z 地址默认八进制 7 位、ASCII 对照用尖括号包裹。核对字节时两者可互换,读对方的输出只需关注数值。
四、实战示例
以下示例均在本环境实测执行。
4.1 快速确认文件类型(魔数)
$ head -c 16 /bin/ls | hexdump -C
00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010
开头 7f 45 4c 46(.ELF)即 ELF 可执行文件魔数;同理可确认 PNG(89 50 4e 47)、ZIP(50 4b)等格式。
4.2 检查文本文件是否混入控制字符
$ printf "name=value\r\n" > crlf.txt
$ hexdump -C crlf.txt
00000000 6e 61 6d 65 3d 76 61 6c 75 65 0d 0a |name=value..|
0000000c
行尾 0d 0a(显示为 ..)表明是 Windows 风格 CRLF 换行;Unix 文件应只有 0a。
4.3 查看文件被截断或损坏的位置
$ hexdump -C -s 8 -n 8 bin16.bin
00000008 6f 72 6c 64 21 0a ff 01 |orld!...|
00000010
-s 跳到目标偏移、-n 限定窗口大小,可直接检查文件任意区段的原始字节,是排查二进制损坏点的基本手法。
4.4 用 -e 输出纯十六进制流(供其他程序消费)
$ hexdump -e "16/1 \"%02x\"" -e "\"\n\"" bin16.bin
48656c6c6f2c20776f726c64210aff01
无空格连排的十六进制串可直接作为哈希比对、协议调试脚本的输入。注意 hexdump 默认会把「重复行」压缩成 *,管道消费时务必加 -v(--no-squeezing,见 2.2 节 help 原文)。
五、注意事项与常见问题
Q1:hexdump 和 od 应该选哪个?
肉眼快速检查用 hexdump -C(视图最直观);脚本化解析、需要多种数值宽度(1/2/4/8 字节、有符号/浮点)时用 od 的 -t 体系。两者字节数值一致(见 3.8 节对照),可互相验证。
Q2:hexdump -x 的数值为什么和文件里的字节顺序「相反」?
两字节视图按 CPU 小端序组合(内存 48 65 显示为 6548)。要逐字节核对用 -C(单字节十六进制)。
Q3:输出里出现 *,中间的内容去哪了?
hexdump 默认把连续完全相同的行压缩为一行 *;需要完整输出时加 -v。处理管道输入或做精确比对时建议始终带 -v。
Q4:hexdump 没装时如何安装?
Ubuntu 22.04 中 hexdump 由 bsdextrautils 包提供(见 2.3 节 dpkg -S 实测),执行 apt-get install bsdextrautils 即可;该包通常随系统默认预装。
Q5:hexdump -s 的偏移能用十六进制写吗?
可以,-s 与 -n 的参数支持 GiB/TiB 等后缀(见 2.2 节 help 原文 Arguments 段);十六进制偏移用 0x 前缀书写。
六、参考链接
- hexdump 手册页:
man hexdump(终端内执行,含 -e 格式语法完整说明) - util-linux 上游源码库:https://github.com/util-linux/util-linux
- 同类工具 od、strings 的详解见本专栏《od 命令详解与参数实测(Ubuntu 22.04)》《strings 命令详解与参数实测(Ubuntu 22.04)》