linux dd命令

dd作用

dd作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。

dd的由来

由来(全称):本来应根据其功能描述“Convert an copy”命名为“cc”,但“cc”已经被用以代表“CComplier”,所以命名为“dd”。

用法:dd [操作数]或:dd 选项

  bs=比特数    一次读写的比特数
  cbs=比特数    一次转换的比特数
  conv=CONVS    依照每个逗号分割的标志列表转换文件
  count=块数    只将指定个块数复制到块
  ibs=比特数    一次读取的比特数(默认:512)
   if=文件    从指定文件中读取
  iflag=符号    按照以逗号分隔的符号列表指定的方式读取
  obs=比特数    一次写入指定比特数(默认:512)
   of=文件    写入到指定文件
  oflag=符号    按照以逗号分隔的符号列表指定的方式写入
  seek=块数    在输出开始处跳过指定的块数
  skip=块数    在输入开始处跳过指定的块数
  status=noxfer    禁止传输统计

块和字节数后可能带有以下的一个或多个后缀:
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.

每个 CONV 符号可能为:

  ascii        由EBCDIC 码转换至ASCII 码
  ebcdic    由ASCII 码转换至EBCDIC 码
  ibm        由ASCII 码转换至替换的EBCDIC 码
  block        将结束字符块里的换行替换成等长的空格
  unblock    将cbs 大小的块中尾部的空格替换为一个换行符
  lcase        将大写字符转换为小写
  ucase        将小写字符转换为大写
  swab        交换每一对输入数据字节
  sync        将每个输入数据块以NUL 字符填满至ibs 的大小;当配合block
         或unblock 时,会以空格代替NUL 字符填充
  excl        fail if the output file already exists
   nocreat    do not create the output file
   notrunc    不截断输出文件
  noerror    读取数据发生错误后仍然继续
  fdatasync    结束前将输出文件数据写入磁盘
  fsync    类似上面,但是元数据也一同写入

FLAG 符号可以是:

  append    追加模式(仅对输出有意义;隐含了conv=notrunc)
   direct    使用直接I/O 存取模式
  directory    除非是目录,否则 directory 失败
  dsync        使用同步I/O 存取模式
  sync        与上者类似,但同时也对元数据生效
  fullblock    为输入积累完整块(仅iflag)
   nonblock    使用无阻塞I/O 存取模式
  noatime    不更新存取时间
  nocache    丢弃缓存数据
  noctty    不根据文件指派控制终端
  nofollow    不跟随链接文件

 

解读:

1. bs ibs obs  cbs

bs: 一次 读写 = ibs+obs
ibs: 一次读
obs: 一次写
cbs: 一次转换

2. if of

if 是读取的文件
of 是输出的文件

3. seek skip

skip 在从if中读取时  跳过多少比特
seek 在写入of时, 跳过多少比特

使用实例

1、将本地的/dev/hdb整盘备份到/dev/hdd

1

dd if=/dev/hdb of=/dev/hdd

2、将/dev/hdb全盘数据备份到指定路径的image文件

1

dd if=/dev/hdb of=/root/image

3、备份/dev/hdb全盘数据,并利用gzip工具进行压缩,保存到指定路径

1

dd if=/dev/hdb | gzip > /root/image.gz

4、把一个文件拆分为3个文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#文件大小为2.3k

[Oracle@rhel6 ~]$ ll db1_db_links.sql

-rw-r--r-- 1 oracle oinstall 2344 Nov 21 10:39 db1_db_links.sql

#把这个文件拆成每个文件1k,bs=1k,count=1,使用skip参数指定在输入文件中跳过多少个bs支读取

[oracle@rhel6 ~]$ dd if=db1_db_links.sql of=dd01.sql bs=1k count=1

1+0 records in

1+0 records out

1024 bytes (1.0 kB) copied, 4.5536e-05 s, 22.5 MB/s

[oracle@rhel6 ~]$ dd if=db1_db_links.sql of=dd02.sql bs=1k count=1 skip=1

1+0 records in

1+0 records out

1024 bytes (1.0 kB) copied, 0.000146387 s, 7.0 MB/s

[oracle@rhel6 ~]$ dd if=db1_db_links.sql of=dd03.sql bs=1k count=1 skip=2

0+1 records in

0+1 records out

296 bytes (296 B) copied, 0.000204216 s, 1.4 MB/s

#拆分出的文件

[oracle@rhel6 ~]$ ll dd*sql

-rw-r--r-- 1 oracle oinstall 1024 May 20 14:58 dd01.sql

-rw-r--r-- 1 oracle oinstall 1024 May 20 14:58 dd02.sql

-rw-r--r-- 1 oracle oinstall 296 May 20 14:58 dd03.sql

5、把拆分出的文件合并为1个?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#合并操作,此时用到seek参数,用于指定在输入文件中跳过的bs数

[oracle@rhel6 ~]$ dd of=1.sql if=dd01.sql

2+0 records in

2+0 records out

1024 bytes (1.0 kB) copied, 0.000176 s, 5.8 MB/s

[oracle@rhel6 ~]$ dd of=1.sql if=dd02.sql bs=1k seek=1

1+0 records in

1+0 records out

1024 bytes (1.0 kB) copied, 0.000124038 s, 8.3 MB/s

[oracle@rhel6 ~]$ dd of=1.sql if=dd03.sql bs=1k seek=2

0+1 records in

0+1 records out

296 bytes (296 B) copied, 0.00203881 s, 145 kB/s

#与拆分前的文件进行校验

[oracle@rhel6 ~]$ diff 1.sql db1_db_links.sql

[oracle@rhel6 ~]$

6、在输出文件中指定的位置插入数据,而不截断输出文件

需要使用conv=notrunc参数

1

[oracle@rhel6 ~]$ dd if=2.sql of=1.sql bs=1k seek=1 count=2 conv=notrunc

————————————————————————————————————————————————————————————————————————————————————————————————————————————————

1. 批量生成随机名称的测试文件

?

1

2

3

4

for i in {10..10} 

do 

dd if=/dev/zero of=junk.test$i bs="$RANDOM"K count=20 

done

2. 备份和恢复

备份

?

1

2

3

dd if=abc.gz of=abc.gz.bak1 bs=1k count=10000 

dd if=abc.gz of=abc.gz.bak2 bs=1k skip=10000 count=70000 

dd if=abc.gz of=abc.gz.bak3 bs=1k skip=80000

恢复方法如下:

?

1

2

3

dd if=abc.gz.bak1 of=abc.gz 

dd if=abc.gz.bak2 of=abc.gz bs=1k seek=10000 

dd if=abc.gz.bak3 of=abc.gz bs=1k seek=80000

3. 转换大小写

生成大写:dd if=dd.txt of=my.log bs=1M count=1 conv=ucase

生成小写:为了不懒惰,留给读者试试

4. 拷贝自己 

复制代码 代码如下:


file_subscript=copy  
dd if=$0 of=$0.$file_subscript 2>/dev/null

 

5.要把一张软盘的内容拷贝到另一张软盘上,利用/tmp作为临时存储区。把源盘插入驱动器中,输入下述命令:

$ dd if =/dev/fd0 of = /tmp/tmpfile

拷贝完成后,将源盘从驱动器中取出,把目标盘插入,输入命令:

$ dd if = /tmp/tmpfile of =/dev/fd0

软盘拷贝完成后,应该将临时文件删除:

$ rm /tmp/tmpfile  

6. 把net.i这个文件写入软盘中,并设定读/写缓冲区的数目。

(注意:软盘中的内容会被完全覆盖掉)

$ dd if = net.i of = /dev/fd0 bs = 16384  

7.将文件sfile拷贝到文件 dfile中。

$ dd if=sfile of=dfile

8.创建一个100M的空文件

dd if=/dev/zero of=hello.txt bs=100M count=1

python实现:

def creatfilesize(n):
    local_time = time.strftime("%Y%m%d%H%M%S",time.localtime())
    file_name = "E:\\testFile\\"+str(local_time)+".txt"
    bigFile= open(file_name, 'w')
    bigFile.seek(1024*1024*n) 
    bigFile.write('test')
    bigFile.close()
if __name__ == '__main__':
    n = input("输入你要生成的文件大小(单位为M):")
    creatfilesize(n)

————————————————————————————————————————————————————————————————————————————————————————————————————————————————

linux的一些基本常识

1. shell脚本以.sh 为扩展名,通常运行 ./${filename}.sh 或者 sh ${filename}.sh
2. shell 脚本开头以 #!/bin/bash #!读作 “shebang”
3. 开启调试  #!/bin/bash –xv
4. /dev/null 任何东西丢进去都会消失,linux黑洞。 /dev/zero 用于初始化,会产生0

/dev/null------它是空设备,也称为位桶(bit bucket)。任何写入它的输出都会被抛弃。如果不想让消息以标准输出显示或写入文件,那么可以将消息重定向到位桶。
/dev/zero------该设备无穷尽地提供0,可以使用任何你需要的数目——设备提供的要多的多。他可以用于向设备或文件写入字符串0。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值