Linux中的dd命令

一、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
[[email protected] ~]$ 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支读取
[[email protected] ~]$ 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
[[email protected] ~]$ 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
[[email protected] ~]$ 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
#拆分出的文件
[[email protected] ~]$ 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数
[[email protected] ~]$ 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
[[email protected] ~]$ 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
[[email protected] ~]$ 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
#与拆分前的文件进行校验
[[email protected] ~]$ diff 1.sql db1_db_links.sql
[[email protected] ~]$

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

需要使用conv=notrunc参数
[[email protected] ~]$ dd if=2.sql of=1.sql bs=1k seek=1 count=2 conv=notrunc

参考:http://blog.csdn.net/xizaihui/article/details/53307578

http://blog.csdn.net/jijiagang/article/details/42192049

时间: 2024-08-28 06:01:32

Linux中的dd命令的相关文章

Linux 下的dd命令使用详解

dd if=/dev/zero of=的含义是什么?Linux 下的dd命令使用详解     一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. if=文件名:输入文件名,缺省为标准输入.即指定源文件.< if=input file > 2. of=文件名:输出文件名,缺省为标准输出.即指定目的文件.< of=output file >

在linux上用dd命令实现ghost功能

转自:http://blog.jobbole.com/90978/ ghost和g4l 安装操作系统,速度太慢,整个过程太冗长乏味了. 安装过程中,需要回答若干问题,系统需要安装无数个软件,创建和写入无数的文件.因为涉及到大量的文件定位和读写,速度一定是快不起来的. Windows下我们常常使用ghost系统来备份和刻录操作系统.ghost可以clone整个系统的镜像,然后在新的电脑上恢复,相当简单.用ghost系统安装操作系统比使用安装光盘安装系统要快捷多了,也不需要回答任何问题了. 那么,我

Linux中的cp命令&老九门

cp命令详解 cp命令的老九门 我们先看第一种情况: 1.源是一个文件,目标是不存在的 使用 cp aa /testdir/dir1他会创建一个dir1的目标文件,并且将源的内容放到创建的dir目标文件中 2.源是一个文件,目标存在且为文件(上述命令执行后,dir1的文件就会被创建了),然后再次执行cp aa /testdir/dir1,会提示你是否覆盖dir1这个文件,选择y后,再次查看aa和dir1文件的属性,发现dir1的mtime发生了改变说明该文件是被修改了,的确完成了复制. 但是使用

linux中的压缩命令详细解析(二)

我们在<Linux中的压缩命令详细解析(一)>中已经讲解了常见的三种压缩命令,下面我们开始讲解工作中最常用到的tar命令. 为了使压缩和解压缩变得简单,tar命令就应运而生了.那么究竟该如何使用呢? tar.gz格式: 压缩命令: tar -zcvf 压缩文件名 源文件名 举例: 把abc文件压缩成后缀为tar.gz格式的文件 tar -zcvf abc.tar.gz abc 解压缩命令: 举例:解压缩abc.tar.gz文件 tar -zxvf abc.tar.gz tar.bz2格式: 压

教你在Linux中如何用命令或手动修改文件来添加一个用户

教你在Linux中如何使用命令或手动修改文件添加一个用户 首先我们从一个例子进行引入:添加一个happy用户,基本组为happy(5200),附加组为luzhi. 一.用命令的方法实现: groupadd -g 5200 happy useradd -u 5200 -g happy -G luzhi  happy passwd happy su - happy 这样就这个用户就创建成功了. 下面来演示一下: 验证系统中是否存在happy用户,从输出看是没有存在happy用户的. 2.我们先建一个

Linux中的In命令

ln是linux中一个非常重要命令.它的功能是为某一个文件在另外一个位置建立一个同步的链接,这个命令最常用的参数是-s,具体用法是: ln -s  源文件 目标文件    -s 是 symbolic的意思. 例:ln  -s  /lib/lsb   /usr/lj即:在usr目录下建立指向/lib/lsb目录的lj文件. 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目录下用ln命令链接(link

linux中的strings命令简介

摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道strings命令的.我们先用man strings来看看: strings - print the strings of printable characters in files. 意思是, 打印文件中可打印的字符.  我来补充一下吧, 这个文件可以是文本文件(test.c), 可执行文件(te

linux中的strings命令简介2

摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 之前我们聊过linux strings的用法和用途, 但据我了解, 还有部分朋友并不常用strings, 这是个不好的习惯. 所以, 本文继续啰嗦一下strings命令. 在软件开发中, 我们经常需要修改代码, 并生成静态库.动态库或者可执行文件, 有时候, 工程太大, 那怎样确定自己改动的代码正确编译到库中去了呢? 用strings命令吧!  

辛星浅析linux中的ac命令

linux中的ac命令根据当前/var/log/wtmp文件中的登录的进入和退出来报告用户连接的时间,默认是以小时为单位,如果不使用标识,则报告的是总时间. 它的主要参数有两个: (1)-d将显示每天的连接时间. (2)-p将显示每个用户的连接时间.