linux 操作系统下dd 命令介绍和使用案例

linux 操作系统下dd 命令介绍和使用案例

1. dd 命令简介

dd 命令是一个功能强大的 Linux 工具,用于转换和复制文件。它的主要用途包括:

  • 创建引导盘
  • 备份和恢复磁盘分区
  • 创建磁盘镜像
  • 清除磁盘数据
  • 测试读写性能

dd 命令的语法与大多数 Linux 命令有所不同,使用 option=value 的形式指定选项。

2. dd 命令常用选项

  • if=FILE: 指定输入文件
  • of=FILE: 指定输出文件
  • bs=BYTES: 设置读写的块大小
  • count=BLOCKS: 仅复制指定数量的块
  • skip=BLOCKS: 跳过输入文件开头的指定数量的块
  • seek=BLOCKS: 跳过输出文件开头的指定数量的块
  • conv=CONVS: 指定文件转换选项

命令选项:

root@meng:~# dd

^Z

1+ Stopped dd

root@meng:~# which dd

/usr/bin/dd

root@meng:~# dd -help

dd: invalid option -- 'h'

Try 'dd --help' for more information.

root@meng:~# dd --help

Usage: dd OPERAND...

or: dd OPTION

Copy a file, converting and formatting according to the operands.

bs=BYTES read and write up to BYTES bytes at a time (default: 512);

overrides ibs and obs

cbs=BYTES convert BYTES bytes at a time

conv=CONVS convert the file as per the comma separated symbol list

count=N copy only N input blocks

ibs=BYTES read up to BYTES bytes at a time (default: 512)

if=FILE read from FILE instead of stdin

iflag=FLAGS read as per the comma separated symbol list

obs=BYTES write BYTES bytes at a time (default: 512)

of=FILE write to FILE instead of stdout

oflag=FLAGS write as per the comma separated symbol list

seek=N skip N obs-sized blocks at start of output

skip=N skip N ibs-sized blocks at start of input

status=LEVEL The LEVEL of information to print to stderr;

'none' suppresses everything but error messages,

'noxfer' suppresses the final transfer statistics,

'progress' shows periodic transfer statistics

N and BYTES may be followed by the following multiplicative suffixes:

c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M,

GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y.

Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

Each CONV symbol may be:

ascii from EBCDIC to ASCII

ebcdic from ASCII to EBCDIC

ibm from ASCII to alternate EBCDIC

block pad newline-terminated records with spaces to cbs-size

unblock replace trailing spaces in cbs-size records with newline

lcase change upper case to lower case

ucase change lower case to upper case

sparse try to seek rather than write all-NUL output blocks

swab swap every pair of input bytes

sync pad every input block with NULs to ibs-size; when used

with block or unblock, pad with spaces rather than NULs

excl fail if the output file already exists

nocreat do not create the output file

notrunc do not truncate the output file

noerror continue after read errors

fdatasync physically write output file data before finishing

fsync likewise, but also write metadata

Each FLAG symbol may be:

append append mode (makes sense only for output; conv=notrunc suggested)

direct use direct I/O for data

directory fail unless a directory

dsync use synchronized I/O for data

sync likewise, but also for metadata

fullblock accumulate full blocks of input (iflag only)

nonblock use non-blocking I/O

noatime do not update access time

nocache Request to drop cache. See also oflag=sync

noctty do not assign controlling terminal from file

nofollow do not follow symlinks

count_bytes treat 'count=N' as a byte count (iflag only)

skip_bytes treat 'skip=N' as a byte count (iflag only)

seek_bytes treat 'seek=N' as a byte count (oflag only)

Sending a USR1 signal to a running 'dd' process makes it

print I/O statistics to standard error and then resume copying.

Options are:

--help display this help and exit

--version output version information and exit

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/dd\>

or available locally via: info '(coreutils) dd invocation'

命令案例:

磁盘测试:

root@meng:~# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS

loop0 7:0 0 63.9M 1 loop /snap/core20/2264

loop1 7:1 0 87M 1 loop /snap/lxd/28373

loop2 7:2 0 63.9M 1 loop /snap/core20/2318

loop3 7:3 0 87M 1 loop /snap/lxd/29351

loop4 7:4 0 38.7M 1 loop /snap/snapd/21465

loop5 7:5 0 38.8M 1 loop /snap/snapd/21759

sda 8:0 0 35G 0 disk

├─sda1 8:1 0 1M 0 part

├─sda2 8:2 0 2G 0 part /boot

└─sda3 8:3 0 33G 0 part

└─ubuntu--vg-ubuntu--lv 253:0 0 33G 0 lvm /

sr0 11:0 1 1024M 0 rom

root@meng:~# dd if=/dev/zero of=/data/test2.img bs=50M count=2 oflag=dsync

2+0 records in

2+0 records out

104857600 bytes (105 MB, 100 MiB) copied, 0.201284 s, 521 MB/s

root@meng:~# dd if=/dev/zero of=/data/test2.img bs=512M count=2 oflag=dsync

2+0 records in

2+0 records out

1073741824 bytes (1.1 GB, 1.0 GiB) copied, 10.2261 s, 105 MB/s

root@meng:~#

清除磁盘数据:

复制代码
bash
dd if=/dev/zero of=/dev/sda bs=1M

使用 /dev/zero 设备擦除 /dev/sda 硬盘的所有数据。

相关推荐
科技象限4 小时前
数据防泄漏系统:文件是怎么溜出门的,就该怎么堵
运维·网络·网络安全·安企神
cpolar技术支持4 小时前
端口被谁占了、这台机上开了哪些监听?本机起一个端口占用只读清单,cpolar 给异地同事远程看谁在监听
运维·python·内网穿透·cpolar·端口占用
JavaPub-rodert4 小时前
Docker 容器为什么 stop 半天停不下来?讲透 PID 1、SIGTERM、SIGKILL、僵尸进程与 tini
运维·docker·容器
编程火箭车5 小时前
MySQL 拥抱 AI:向量、RAG、AutoML 全进了数据库
数据库·mysql·ai编程·向量数据库·rag·automl·mysql9
用户8662978480085 小时前
KingbaseES 中文乱码、排序不对?区域和字符集的问题,一次说清
数据库
学IT的周星星5 小时前
记忆系统:让 Agent 记住你是谁、上次聊到哪
运维·服务器·人工智能·windows·大模型·llm·agent
aruo5 小时前
回退旧镜像之前,先确认数据库还能兼容
数据库
en.en..5 小时前
MRS/MSR 指令操作 xPSR 寄存器
linux·运维·服务器
张洛闻Eren5 小时前
Ansible【第三课】:管理变量和事实
数据库·oracle
Brilliantwxx5 小时前
【Linux】 自主shell编写巩固复习篇
linux·运维·服务器·开发语言