Linux od命令教程:如何以各种格式转储文件(附案例详解和注意事项)

Linux od命令介绍

od命令在Linux中的全称是"octal dump",它被用来以八进制和其他格式(包括十六进制、十进制和ASCII)转储文件。这个命令在你需要查看通常不易读的文件时非常有用,比如已经编译过的二进制文件。

Linux od命令适用的Linux版本

od命令在所有主流的Linux发行版中都可以使用,包括但不限于Ubuntu、Debian、Fedora、RHEL、SUSE、Arch Linux等。它是coreutils包的一部分,这个包在几乎所有的Linux发行版中都被预装了。

如果没有可以按照下面方法安装:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo apt-get install coreutils  # 对于Debian/Ubuntu
[linux@bashcommandnotfound.cn ~]$ sudo yum install coreutils  # 对于CentOS/RHEL
[linux@bashcommandnotfound.cn ~]$ sudo dnf install coreutils  # 对于Fedora

安装好后查看版本

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od --version
od (GNU coreutils) 8.30

Linux od命令的基本语法

语法格式如下:

bash 复制代码
od [OPTION]... [FILE]...

Linux od命令的常用选项或参数说明

以下是一些od命令的常用选项:

选项 描述
-b 以八进制格式显示输入的内容
-c 以字符格式显示输入的内容
-x 将输入转换为十六进制格式
-d 将输入转换为十进制格式
-j 跳过文件的初始字节数
-N 限制输出的字节数
-w 自定义输出的宽度
-v 输出重复的值

Linux od命令的实例

1. 以八进制格式显示文件的内容:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ echo "Hello, World!" > file.txt
[linux@bashcommandnotfound.cn ~]$ od -b file.txt
0000000 110 145 154 154 157 054 040 127 157 162 154 144 041 012
0000016

2. 以字符格式显示文件的内容:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od -c file.txt
0000000   H   e   l   l   o   ,       W   o   r   l   d   !  \n
0000015

3. 以十六进制格式显示文件的内容:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od -x file.txt
0000000 6548 6c6c 2c6f 5720 726f 646c 210a
0000015

4. 跳过文件的初始字节数:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od -j 5 -c file.txt
0000005   ,       W   o   r   l   d   !  \n
0000012

5. 以十进制格式显示文件的内容:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od -d file.txt
0000000 25928 27756 11776 28516 27756 26480  8225
0000015

6. 显示文件的前四个字节:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od -N 4 -c file.txt
0000000   H   e   l   l
0000004

7. 自定义输出的宽度:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ od -w1 -c file.txt
0000000   H
0000001   e
0000002   l
0000003   l
0000004   o
0000005   ,
0000006    
0000007   W
0000010   o
0000011   r
0000012   l
0000013   d
0000014   !
0000015  \n
0000016

8. 输出重复的值:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ echo -e "Hello, World!\nHello, World!" > file.txt
[linux@bashcommandnotfound.cn ~]$ od -v -c file.txt
0000000   H   e   l   l   o   ,       W   o   r   l   d   !  \n   H   e
0000020   l   l   o   ,       W   o   r   l   d   !  \n
0000034

Linux od命令的注意事项

如果你在使用od命令时遇到了bash: od: command not found的错误,那么你可能需要安装coreutils包。在大多数Linux发行版中,你可以使用下面的命令来安装它:

更多详细内容可以参考:
linux入门学习教程 - Linux入门自学网
Linux下od命令用法详解:如何查看文件中的字节数据和隐藏信息

相关推荐
汪汪大队u3 分钟前
从 Docker Compose 到 Kubernetes:物联网管理系统迁移实战(3)—— 两个运维坑
运维·docker·kubernetes
小赵不会秃头3 分钟前
数据结构Day 06:线性结构、库操作及 Makefile 完整学习笔记
java·linux·数据结构·算法·面试
雨田大大4 分钟前
Windows11下IDEA运行后端时,端口被占用的解决方法
linux·运维·服务器
IKun-bug7 分钟前
CentOS 7 安装 Claude Code 指南
linux·运维·centos
上海云盾-小余10 分钟前
服务器入侵应急处置:痕迹清理、漏洞封堵与事后加固全流程
运维·服务器
kdxiaojie13 分钟前
U-Boot分析【学习笔记】(8)
linux·笔记·学习
风曦Kisaki17 分钟前
# Linux运维Day02:LNMP架构部署、动静分离原理、Nginx地址重写、systemd服务管理
linux·运维·架构
Shadow(⊙o⊙)17 分钟前
Linux进程地址空间——钻入Linux内核架构性剖析 硬核手搓!
java·linux·运维·服务器·开发语言·c++
大明者省17 分钟前
乌邦托服务器系统www不同文件夹bird、infra建立隔离的虚拟环境
linux·运维·服务器
MXsoft61820 分钟前
**降本增效两不误:精细化运维助力业务持续增长**
运维