欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux中dd命令使用实例教程

程序员文章站 2023-11-11 13:45:58
本文主要给大家介绍了关于linux中dd命令使用的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 一、linux dd命令用指定大小的块拷贝一个文件,并在拷贝的...

本文主要给大家介绍了关于linux中dd命令使用的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:

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

使用方法:dd [operand]

参数注释:

 bs=bytes  read and write bytes bytes at a time (also see ibs=,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 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=blocks  skip blocks obs-sized blocks at start of output
 skip=blocks  skip blocks ibs-sized blocks at start of input
 status=which which info to suppress outputting to stderr;
     'noxfer' suppresses transfer stats, 'none' suppresses all

convs的可选参数

 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
 nocreat do not create the output file
 excl  fail if the output file already exists
 notrunc do not truncate the output file
 ucase  change lower case to upper case
 sparse try to seek rather than write the output for nul input blocks
 swab  swap every pair of input bytes
 noerror continue after read errors
 sync  pad every input block with nuls to ibs-size; when used
   with block or unblock, pad with spaces rather than nuls
 fdatasync physically write output file data before finishing
 fsync  likewise, but also write metadata

flags的可选参数

 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
 noctty do not assign controlling terminal from file
 nofollow do not follow symlinks
 count_bytes treat 'count=n' as a byte count (iflag only)

注意:指定数字的地*以下列字符结尾,则乘以相应的数字:

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

二、使用实例

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

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

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

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

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

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

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

#文件大小为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个

#合并操作,此时用到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参数

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。