Linux中dd命令使用实例教程

 更新时间:2017年05月21日 11:29:32   作者:hbxztc  
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

总结

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

相关文章

  • CentOS安装jdk的几种方法及配置环境变量方式

    CentOS安装jdk的几种方法及配置环境变量方式

    这篇文章主要介绍了CentOS安装jdk的几种方法及配置环境变量方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • Gunicorn运行与配置方法

    Gunicorn运行与配置方法

    这篇文章主要介绍了Gunicorn运行与配置方法,使用pre-fork worker模式,具有使用非常简单,轻量级的资源消耗,以及高性能等特点。对此感兴趣的朋友跟随小编一起看看吧
    2019-08-08
  • Linux系统设置tomcat开机自启介绍

    Linux系统设置tomcat开机自启介绍

    大家好,本篇文章主要讲的是Linux系统设置tomcat开机自启介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Linux环境下快速搭建ftp服务器方法介绍

    Linux环境下快速搭建ftp服务器方法介绍

    这篇文章主要介绍了Linux环境下快速搭建ftp服务器方法介绍,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • RHEL7使用ssm命令管理LVM的方法

    RHEL7使用ssm命令管理LVM的方法

    下面小编就为大家带来一篇RHEL7使用ssm命令管理LVM的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Linux使用ncdu查看磁盘使用的操作详解

    Linux使用ncdu查看磁盘使用的操作详解

    ncdu (NCurses Disk Usage) 是一个用于 Linux 和类 unix 系统的磁盘实用程序,它提供了一种比 du 等传统命令更具交互性和用户友好性的方式来查看和分析磁盘空间使用情况,本文给大家介绍了Linux使用ncdu查看磁盘使用的操作,需要的朋友可以参考下
    2025-02-02
  • ubuntu 系统上为php加上redis 扩展的实现方法

    ubuntu 系统上为php加上redis 扩展的实现方法

    这篇文章主要介绍了ubuntu 系统上为php加上redis 扩展的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
    2017-10-10
  • Linux中别名与二进制的使用教程

    Linux中别名与二进制的使用教程

    这篇文章主要给大家介绍了关于Linux中别名与二进制的使用方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-05-05
  • Linux中网络性能优化与监控实战详细指南

    Linux中网络性能优化与监控实战详细指南

    在高并发场景下,Linux服务器的网络性能直接影响用户体验,这篇文章将全面解析Linux网络性能优化的核心方法,感兴趣的小伙伴可以学习一下
    2025-04-04
  • Linux外围文件系统的定制方法

    Linux外围文件系统的定制方法

    这篇文章主要给大家介绍了关于Linux外围文件系统的定制方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-02-02

最新评论