Linux dd命令详解

一、dd命令介绍

  dd命令可以用指定大小的块来拷贝一个文件,并在拷贝的同时进行指定的转换。注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512;c=1;k=1024;w=2

参数注释:

  • if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input file >
  • of=文件名:输出文件名,缺省为标准输出。即指定目的文件。< of=output file >
  • ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。
  • obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。
  • bs=bytes:同时设置读入/输出的块大小为bytes个字节。
  • cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。
  • skip=blocks:从输入文件开头跳过blocks个块后再开始复制。
  • seek=blocks:从输出文件开头跳过blocks个块后再开始输出。
    • 注意:通常只用当输出文件是磁盘或磁带时才有效,即备份到磁盘或磁带时才有效。
  • count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。
  • conv=conversion:用指定的参数转换文件。
    • ascii:转换ebcdic为ascii
    • ebcdic:转换ascii为ebcdic
    • ibm:转换ascii为alternate ebcdic
    • block:把每一行转换为长度为cbs,不足部分用空格填充
    • unblock:使每一行的长度都为cbs,不足部分用空格填充
    • lcase:把大写字符转换为小写字符
    • ucase:把小写字符转换为大写字符
    • swab:交换输入的每对字节
    • noerror:出错时不停止
    • notrunc:不截短输出文件
    • sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

二、dd命令使用实例

【例1】拷贝文件(使用dd命令)

➜  test cat data2.txt
line1:This is the header line 1.
line2:This is the first data line 2.
line3:This is the second DATA line 3.
line4:This is the last line 4.
➜ test
dd if=data2.txt of=data3.txt #data3.txt是不存在的,此时会创建文件 0+1 records in 0+1 records out 139 bytes copied, 4.7933e-05 s, 2.9 MB/s
➜ test
cat data3.txt #成功拷贝 line1:This is the header line 1. line2:This is the first data line 2. line3:This is the second DATA line 3. line4:This is the last line 4.

【例2】拷贝文件,并使用gzip工具进行压缩

➜  test dd if=data2.txt | gzip > data4.gz
0+1 records in
0+1 records out
139 bytes copied, 5.451e-05 s, 2.5 MB/s
➜ test ll total 36K
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Oct 7 11:18 data3.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 101 Oct 7 11:39 data4.gz -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 21 18:08 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 113 Sep 27 10:17 test.txt

【例3】将压缩的备份文件(data4.gz)恢复到指定盘

➜  test gzip -dc data4.gz | dd of=./hdb
0+1 records in
0+1 records out
139 bytes copied, 7.2875e-05 s, 1.9 MB/s
➜ test ll total 40K
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Oct 7 11:18 data3.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 101 Oct 7 11:39 data4.gz -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Oct 7 11:43 hdb -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 21 18:08 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 113 Sep 27 10:17 test.txt
➜ test
cat hdb line1:This is the header line 1. line2:This is the first data line 2. line3:This is the second DATA line 3. line4:This is the last line 4.

【例4】拷贝文件,同时指定拷贝的块数和块大小

dd if=/dev/hda of=/root/image count=1 bs=512

count=1指仅拷贝一个块;bs=512指块大小为512个字节。

【例5】将一个很大的视频文件中的第i个字节的值改成0x41(也就是大写字母A的ASCII值)

echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc
原文地址:https://www.cnblogs.com/baichunyu/p/15375379.html