00. 目录
01. 命令概述
od命令用于输出文件的八进制、十六进制或其它格式编码的字节,通常用于显示或查看文件中不能直接显示在终端的字符。
常见的文件为文本文件和二进制文件。此命令主要用来查看保存在二进制文件中的值。比如,程序可能输出大量的数据记录,每个数据是一个单精度浮点数。这些数据记录存放在一个文件中,如果想查看下这个数据,这时候od命令就派上用场了。在我看来,od命令主要用来格式化输出文件数据,即对文件中的数据进行无二义性的解释。不管是IEEE754格式的浮点数还是ASCII码,od命令都能按照需求输出它们的值。
02. 命令格式
用法:od [选项]... [文件]...
或:od [-abcdfilosx]... [文件] [[+]偏移量[.][b]]
或:od --traditional [选项]... [文件] [[+]偏移量[.][b] [+][标签][.][b]]
- 1
- 2
- 3
03. 常用选项
-a 此参数的效果和同时指定”-ta”参数相同
-A 选择要以何种基数计算字码
-b 此参数的效果和同时指定”-toC”参数相同
-c 此参数的效果和同时指定”-tC”参数相同
-d 此参数的效果和同时指定”-tu2″参数相同
-f 此参数的效果和同时指定”-tfF”参数相同
-h 此参数的效果和同时指定”-tx2″参数相同
-i 此参数的效果和同时指定”-td2″参数相同
-j<字符数目> 略过设置的字符数目
-l 此参数的效果和同时指定”-td4″参数相同
-N<字符数目> 到设置的字符数目为止
-o 此参数的效果和同时指定”-to2″参数相同
-s<字符串字符数> 只显示符合指定的字符数目的字符串
-t<输出格式> 设置输出格式
-v 输出时不省略重复的数据
-w<每列字符数> 设置每列的最大字符数
-x 此参数的效果和同时指定”-h”参数相同
--help 查看帮助信息
--version 显示版本信息
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
04. 参考示例
4.1 八进制显示文件内容
[deng@localhost tmp]$ echo "hello world" > tmp
[deng@localhost tmp]$ cat tmp
hello world
[deng@localhost tmp]$ od -b tmp
0000000 150 145 154 154 157 040 167 157 162 154 144 012
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
说明:使用单字节八进制解释进行输出,注意左侧的默认地址格式为八字节
4.2 使用ASCII码进行输出
[deng@localhost tmp]$ od -c tmp
0000000 h e l l o w o r l d \n
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
说明:使用ASCII码进行输出,注意其中包括转义字符
4.3 使用单字节十进制进行解释
[deng@localhost tmp]$ od -t d1 tmp
0000000 104 101 108 108 111 32 119 111 114 108 100 10
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
4.4 设置地址格式为十进制
[deng@localhost tmp]$ od -A d -c tmp
0000000 h e l l o w o r l d \n
0000012
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
4.5 设置地址格式为十六进制
[deng@localhost tmp]$ od -A x -c tmp
000000 h e l l o w o r l d \n
00000c
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
4.6 跳过开始的两个字节
00000c
[deng@localhost tmp]$ od -j 2 -c tmp
0000002 l l o w o r l d \n
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
4.7 跳过开始的两个字节,并且仅输出两个字节
[deng@localhost tmp]$ od -N 2 -j 2 -c tmp
0000002 l l
0000004
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
4.8 每行仅输出1个字节
[deng@localhost tmp]$ od -w1 -c tmp
0000000 h
0000001 e
0000002 l
*
0000004 o
0000005
0000006 w
0000007 o
0000010 r
0000011 l
0000012 d
0000013 \n
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
4.9 每行输出两个字节
[deng@localhost tmp]$ od -w2 -c tmp
0000000 h e
0000002 l l
0000004 o
0000006 w o
0000010 r l
0000012 d \n
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4.10 每行输出3个字节,并使用八进制单字节进行解释
[deng@localhost tmp]$ od -w3 -b tmp
0000000 150 145 154
0000003 154 157 040
0000006 167 157 162
0000011 154 144 012
0000014
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
评论记录:
回复评论: