od 命令详解与参数实测(Ubuntu 22.04)

od 字节转储详解:-c/-t x1z/-A/-N/-j/-w 各视图与小写 -s、大写 -S 的区别,Ubuntu 22.04 实测

Linux命令Linux系统UbuntuUbuntu 22.04命令行文本处理2026/10/15

od 命令详解与参数实测(Ubuntu 22.04)

目标:全面掌握 GNU od(octal dump,八进制转储)的每一个常用参数——-c 字符转义视图、-t x1z 十六进制加 ASCII 尾注、-t d4/-x/-d/-o/-b 五种传统数值视图、-A 地址进制切换、-N 限长、-j 跳过、-w 行宽,并实测厘清小写 -s(两字节十进制)与大写 -S(字符串提取)的区别,全部命令与回显已在 Ubuntu 22.04(GNU coreutils 8.32)实测通过。

一、环境信息

本篇所有命令与回显均在以下环境实测采集,读者在同等版本环境可逐条复现。

  • 操作系统:Ubuntu 22.04.5 LTS(x86_64)
  • od 版本:od(GNU coreutils)8.32(od --version 实测),coreutils 基础工具集,Ubuntu 22.04 默认预装,无需额外安装
  • 配套工具:xxd(字节构成对照)、strings(字符串提取对照工具)
  • 演示目录:/root/demo/od/
  • 测试数据:bin16.bin(16 字节二进制样例,内容为 Hello, world!\n 共 14 字节加 0xFF 0x01 两个字节)、text2.txt(3 字节文本 AB\n)、strmix.bin(NUL 分隔的字符串样例)、neg.bin(2 字节有符号数演示)

二、命令概览

od 的职责是把文件或管道输入按用户指定的格式逐字节转储(dump):默认以八进制显示,可通过 -t 指定十六进制、十进制、浮点、字符等任意格式,是分析二进制文件、检查隐藏字符、逆向定位字节的基础工具。与 hexdump 相比,od 的格式体系(-t TYPE)更完整,是 POSIX 标准工具。

💡 技术拓展:Linux 下常用的字节级查看工具对比——

工具来源特点适用场景
odGNU coreutils格式体系最全(d/o/x/f/c/a + 宽度),POSIX 标准脚本化分析、任意进制转储
hexdumputil-linux-C 经典 hex+ASCII 视图直观,支持 -e 自定义格式快速肉眼检查二进制
xxdvim-common自带 -r 还原功能,可从 hex 反写回二进制hex 编辑与回写
stringsGNU binutils只提取可打印字符串找二进制里的文本线索

本文以 od 为例;快速肉眼看二进制可用 hexdump -C(输出对照见 3.5 节与 hexdump 篇)。

2.1 语法与测试数据准备

od 的语法源自 --help 原文:od [OPTION]... [FILE]...,另有两种传统调用形态。最常用的是 od -t 格式 文件 与 命令 | od -t 格式。先准备贯穿全篇的 16 字节二进制样例(内容可读前 14 字节 + 2 个不可见字节,便于同时观察字符与数值视图):

mkdir -p /root/demo/od
cd /root/demo/od
printf 'Hello, world!\n\xff\x01' > bin16.bin
printf 'AB\n' > text2.txt
printf 'abc\x00defg\x00h' > strmix.bin
printf '\x01\xff' > neg.bin
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

先用 xxd 确认 bin16.bin 的字节构成,作为后续所有视图的对照基准:

$ xxd bin16.bin
00000000: 4865 6c6c 6f2c 2077 6f72 6c64 210a ff01  Hello, world!...

16 字节依次是 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01,即 Hello, world!(13 字符)+ 换行 0a + ff 01。

2.2 --help 全量回显

od --help 输出不长,完整保留(其中 TYPE 规格表是理解 -t 参数的关键):

Usage: od [OPTION]... [FILE]...
  or:  od [-abcdfilosx]... [FILE] [[+]OFFSET[.][b]]
  or:  od --traditional [OPTION]... [FILE] [[+]OFFSET[.][b] [+][LABEL][.][b]]

Write an unambiguous representation, octal bytes by default,
of FILE to standard output.  With more than one FILE argument,
concatenate them in the listed order to form the input.

With no FILE, or when FILE is -, read standard input.

If first and second call formats both apply, the second format is assumed
if the last operand begins with + or (if there are 2 operands) a digit.
An OFFSET operand means -j OFFSET.  LABEL is the pseudo-address
at first byte printed, incremented when dump is progressing.
For OFFSET and LABEL, a 0x or 0X prefix indicates hexadecimal;
suffixes may be . for octal and b for multiply by 512.

Mandatory arguments to long options are mandatory for short options too.
  -A, --address-radix=RADIX   output format for file offsets; RADIX is one
                                of [doxn], for Decimal, Octal, Hex or None
      --endian={big|little}   swap input bytes according the specified order
  -j, --skip-bytes=BYTES      skip BYTES input bytes first
  -N, --read-bytes=BYTES      limit dump to BYTES input bytes
  -S BYTES, --strings[=BYTES]  output strings of at least BYTES graphic chars;
                                3 is implied when BYTES is not specified
  -t, --format=TYPE           select output format or formats
  -v, --output-duplicates     do not use * to mark line suppression
  -w[BYTES], --width[=BYTES]  output BYTES bytes per output line;
                                32 is implied when BYTES is not specified
      --traditional           accept arguments in third form above
      --help     display this help and exit
      --version  output version information and exit


Traditional format specifications may be intermixed; they accumulate:
  -a   same as -t a,  select named characters, ignoring high-order bit
  -b   same as -t o1, select octal bytes
  -c   same as -t c,  select printable characters or backslash escapes
  -d   same as -t u2, select unsigned decimal 2-byte units
  -f   same as -t fF, select floats
  -i   same as -t dI, select decimal ints
  -l   same as -t dL, select decimal longs
  -o   same as -t o2, select octal 2-byte units
  -s   same as -t d2, select decimal 2-byte units
  -x   same as -t x2, select hexadecimal 2-byte units


TYPE is made up of one or more of these specifications:
  a          named character, ignoring high-order bit
  c          printable character or backslash escape
  d[SIZE]    signed decimal, SIZE bytes per integer
  f[SIZE]    floating point, SIZE bytes per float
  o[SIZE]    octal, SIZE bytes per integer
  u[SIZE]    unsigned decimal, SIZE bytes per integer
  x[SIZE]    hexadecimal, SIZE bytes per integer

SIZE is a number.  For TYPE in [doux], SIZE may also be C for
sizeof(char), S for sizeof(short), I for sizeof(int) or L for
sizeof(long).  If TYPE is f, SIZE may also be F for sizeof(float), D
for sizeof(double) or L for sizeof(long double).

Adding a z suffix to any type displays printable characters at the end of
each output line.


BYTES is hex with 0x or 0X prefix, and may have a multiplier suffix:
  b    512
  KB   1000
  K    1024
  MB   1000*1000
  M    1024*1024
and so on for G, T, P, E, Z, Y.
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/od>
or available locally via: info '(coreutils) od invocation'

关键信息解读:小写传统短选项(-b -c -d -o -s -x 等)是 -t 各类格式的等价简写,注意 -s 的传统含义是 -t d2(两字节有符号十进制),不是字符串提取——字符串提取是大写 -S(见 3.15/3.16 节实测)。

2.3 版本确认

$ od --version
od (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://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.

Written by Jim Meyering.

三、参数逐项实测

本章所有实测基于 2.1 节的 bin16.bin(16 字节),输出中每行开头的 0000000、0000020 是八进制地址(默认地址进制为八进制,可用 -A 更改)。

3.1 -c:可打印字符与转义序列视图

$ od -c bin16.bin
0000000   H   e   l   l   o   ,       w   o   r   l   d   !  \n 377 001
0000020

$ od -c text2.txt
0000000   A   B  \n
0000003

说明:可打印字符直接显示(每个字符占 4 列),不可打印字符显示为 C 语言风格的转义序列(\n 换行),其余以八进制显示(377 即 0xFF、001 即 0x01)。末行 0000020 是文件结束地址(八进制 20 = 十进制 16,正好是文件大小)。

3.2 -t x1z:单字节十六进制 + ASCII 尾注(最常用)

$ 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

说明:x1 表示十六进制、每个整数 1 字节;z 后缀在行尾追加可打印字符对照(用 > < 包裹,不可打印字符显示为 .)。这是日常查看二进制最顺手的组合。对照 2.1 节 xxd 输出,字节数值完全一致。

💡 提示:-t x1z 是二进制排查的「默认起手式」——十六进制原始值与 ASCII 尾注一屏同看,认文本、找魔数都先用它;与 hexdump -C 视图的对照见《hexdump 命令详解与参数实测(Ubuntu 22.04)》。

3.3 -t d4:4 字节有符号十进制

$ od -t d4 bin16.bin
0000000  1819043144  1998597231  1684828783    33491489
0000020

说明:把 16 字节按每 4 字节一个有符号整数(小端序)解析。第一组 48 65 6c 6c 小端读为 0x6c6c6548 = 1819043144。SIZE 可换 1/2/4/8,或用 C/S/I/L 表示 sizeof 值。

3.4 -x / -d / -o / -b:四个传统数值视图

-x 等价 -t x2(两字节十六进制,注意字节对按小端反转显示——内存里 48 65 显示为 6548):

$ od -x bin16.bin
0000000 6548 6c6c 2c6f 7720 726f 646c 0a21 01ff
0000020

-d 等价 -t u2(两字节无符号十进制):

$ od -d bin16.bin
0000000 25928 27756 11375 30496 29295 25708  2593   511
0000020

-o 等价 -t o2(两字节八进制):

$ od -o bin16.bin
0000000 062510 066154 026157 073440 071157 062154 005041 000777
0000020

-b 等价 -t o1(单字节八进制,od 的默认风格):

$ od -b bin16.bin
0000000 110 145 154 154 157 054 040 167 157 162 154 144 041 012 377 001
0000020

💡 说明:四个选项都是两字节/单字节的「数值视图」,与 3.2 的 -t x1z 区别在于单位宽度与进制。两字节视图存在小端反转的显示效果,核对具体字节时优先用 -t x1。

3.5 -A:切换地址进制(x / d / n)

默认地址是八进制。-A x 十六进制地址(宽度 6)、-A d 十进制地址(宽度 7)、-A n 不显示地址:

$ od -A x -t x1z bin16.bin
000000 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01  >Hello, world!...<
000010

$ od -A d -t x1z bin16.bin
0000000 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01  >Hello, world!...<
0000016

$ od -A n -t x1z bin16.bin
 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a ff 01  >Hello, world!...<

说明:-A n 适合把输出交给其他程序继续处理(无地址干扰);十六进制地址 000010 即十进制 16。

3.6 -N:限制转储字节数

$ od -N 8 -t x1z bin16.bin
0000000 48 65 6c 6c 6f 2c 20 77                          >Hello, w<
0000010

说明:只读前 8 字节,末行地址 0000010(八进制 10 = 十进制 8)。与 -j 配合可转储文件任意区段。

3.7 -j:跳过起始字节

$ od -j 4 -N 8 -t x1z bin16.bin
0000004 6f 2c 20 77 6f 72 6c 64                          >o, world<
0000014

说明:从第 4 字节开始(地址从 0000004 起算,跳过的字节计入地址),读 8 字节。-j 支持乘数后缀(b=512、K=1024 等,见 2.2 节 BYTES 段原文)。

3.8 -w:指定每行输出宽度

$ od -w8 -t x1z bin16.bin
0000000 48 65 6c 6c 6f 2c 20 77  >Hello, w<
0000010 6f 72 6c 64 21 0a ff 01  >orld!...<
0000020

$ od -w4 -t x1 bin16.bin
0000000 48 65 6c 6c
0000004 6f 2c 20 77
0000010 6f 72 6c 64
0000014 21 0a ff 01
0000020

说明:默认每行 32 字节(见 2.2 节 -w[BYTES] 原文),-w8/-w4 分别改为每行 8/4 字节,16 字节文件相应分成 2 行/4 行。

3.9 -s(小写):两字节有符号十进制——不是字符串!

这是 od 最容易踩的坑。小写 -s 按 2.2 节 help 原文是 same as -t d2,即两字节有符号十进制:

$ od -s bin16.bin
0000000  25928  27756  11375  30496  29295  25708   2593    511
0000020

$ od -s strmix.bin
0000000  25185     99  25956  26470  26624
0000012

strmix.bin 的输出可验证:abc 的字节是 61 62 63,小端组两字节 0x6261 = 25185,正是第一个数字——它输出的是数值而非字符串。

3.10 -S(大写):提取至少 N 个图形字符的字符串

大写 -S 才是字符串提取。实测 16 字节样例与 NUL 分隔样例:

$ od -S 3 bin16.bin

$ od -S 3 strmix.bin
0000000 abc
0000004 defg

说明:strmix.bin(abc\0defg\0h)正确提取出 abc、defg(末尾单字符 h 不足 3 被略去);而 bin16.bin 输出为空——实测 GNU od 的 -S 只提取 NUL(\0)分隔的字符串,以换行结尾的普通文本行不会被提取(printf 'Hello, world!\n' | od -S 3 同样为空,已实测)。日常提取字符串请用 strings 命令(见本专栏 strings 篇)。

3.11 有符号 d2 与无符号 u2 对照

用 2 字节样例 0x01 0xff(小端组为 0xff01)实测同一个字节的两种解读:

$ od -t x2 neg.bin
0000000 ff01
0000002

$ od -t d2 neg.bin
0000000   -255
0000002

$ od -t u2 neg.bin
0000000 65281
0000002

说明:同一对字节,十六进制视图 ff01、有符号十进制 -255(最高位为符号位)、无符号十进制 65281。分析含符号数值的二进制格式(如 int16)时必须选对 d/u。

四、实战示例

以下示例均在本环境实测执行。

4.1 检查文本文件里的隐藏字符

排查「看着一样却比对失败」的配置文件,常因混入控制字符:

$ printf "name=value\r\n" > crlf.txt
$ od -c crlf.txt
0000000   n   a   m   e   =   v   a   l   u   e  \r  \n
0000014

\r(回车符)暴露了 Windows 风格换行——这是 od 排查行尾问题最直接的用法。

4.2 查看文件头部的魔数判断文件类型

$ head -c 16 /bin/ls | od -A x -t x1z
000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00  >.ELF............<

开头 7f 45 4c 46 即 0x7F + "ELF",是 ELF 可执行文件的标准魔数。

4.3 按浮点格式解读二进制数据

$ printf "\x00\x00\x80\x3f" > one.bin

$ od -t f4 one.bin
0000000               1
0000004

4 字节 00 00 80 3f 按 IEEE 754 单精度浮点(小端)解读正好是 1.0,可用于核对二进制数据格式。

4.4 与 strings 配合定位字符串的十六进制偏移

$ strings -t x strmix.bin
      4 defg

$ od -j 4 -N 4 -t x1z strmix.bin
0000004 64 65 66 67                                      >defg<
0000010

先用 strings 拿到 defg 的十六进制偏移 4(默认阈值 4 个字符,偏移 0 处的 abc 因只有 3 字符未显示),再用 od 精确转储该位置的原始字节 64 65 66 67,两者配合完成「字符串线索 → 原始字节」的验证闭环。

五、注意事项与常见问题

Q1:od -s 文件 为什么输出一堆数字而不是字符串? 小写 -s 在 GNU od 里是传统格式 -t d2(两字节有符号十进制,见 2.2 节 help 原文),不是字符串提取;字符串提取是大写 -S BYTES。这是 BSD 传统选项的历史包袱,务必区分大小写。

Q2:od -S 3 对文本文件为什么什么都提取不到? 实测 GNU od 的 -S 只把 NUL(\0)分隔的字节串当作字符串(见 3.10 节:strmix.bin 能提取、以换行结尾的 bin16.bin 为空)。提取普通文本文件中的字符串应使用 strings 命令。

Q3:为什么 -x/-d/-o 的两字节视图数值看起来「字节反了」? 这些视图按 CPU 小端序把两个字节组合成一个数值再显示(内存 48 65 显示为十六进制 6548)。想逐字节核对请用 -t x1。

Q4:输出中间出现 * 是什么意思? od 默认把连续相同的行压缩为一行 *,这是省略标记;要完整输出加 -v(--output-duplicates,见 2.2 节 help 原文)。

Q5:od 默认输出是八进制,为什么日常很少有人直接 od 文件? 八进制是历史传统(源自 PDP-11 时代),现代分析更常用 -t x1z(十六进制加 ASCII 尾注)或 hexdump -C;直接 od 文件 得到的单字节八进制流可读性差,不建议作为日常视图。

六、参考链接