Linux每日一篇 - 61 - dd
Linux数据复制神器!掌握dd命令,精确复制转换数据,创建镜像文件!
dd命令是什么?
dd是”Convert and Copy”的缩写,是一个强大的命令行工具,用于转换和复制文件,常用于创建磁盘镜像、备份系统、测试磁盘性能等操作。
基本用法
# 基本格式
dd if=输入文件 of=输出文件 [参数]
# 指定块大小和数量
dd if=source of=destination bs=512 count=4
# 指定输入输出块大小
dd if=source of=destination ibs=512 obs=1024
# 显示进度
dd if=source of=destination status=progress
实用技巧
# 创建磁盘镜像
dd if=/dev/sda of=disk.img bs=4M status=progress
# 克隆磁盘
dd if=/dev/sda of=/dev/sdb bs=4M status=progress
# 创建指定大小的空文件
dd if=/dev/zero of=largefile.txt bs=1M count=100
# 创建随机数据文件
dd if=/dev/urandom of=random.txt bs=1M count=10
# 清空磁盘数据
dd if=/dev/zero of=/dev/sdX bs=1M status=progress
# 从指定偏移量开始复制
dd if=source of=destination bs=1 skip=10 seek=5 count=20
# 仅复制文件头
dd if=largefile.txt of=header.txt bs=1 count=512
常用场景
# 创建USB启动盘
dd if=ubuntu.iso of=/dev/sdb bs=4M status=progress && sync
# 创建swap文件
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
# 测试磁盘写入速度
dd if=/dev/zero of=testfile bs=1G count=1 oflag=dsync
# 恢复系统镜像
dd if=backup.img of=/dev/sda bs=4M status=progress
# 创建加密分区备份
dd if=/dev/sda1 | gzip > encrypted_backup.img.gz
# 检查磁盘是否有坏扇区
dd if=/dev/sda of=/dev/null bs=1M status=progress
# 从备份恢复分区
dd if=partition_backup.img of=/dev/sda1 bs=4M status=progress