Linux中的dd命令

简介:

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

使用方法:dd [OPERAND]

参数注释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   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的可选参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   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的可选参数

1
2
3
4
5
6
7
8
9
10
11
   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

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、在输出文件中指定的位置插入数据,而不截断输出文件

1
2
需要使用conv=notrunc参数
[oracle@rhel6 ~]$  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





      本文转自hbxztc 51CTO博客,原文链接:http://blog.51cto.com/hbxztc/1927774,如需转载请自行联系原作者



相关文章
|
3月前
|
Linux 网络安全 数据安全/隐私保护
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
320 8
|
3月前
|
监控 Linux
如何检查 Linux 内存使用量是否耗尽?这 5 个命令堪称绝了!
本文介绍了在Linux系统中检查内存使用情况的5个常用命令:`free`、`top`、`vmstat`、`pidstat` 和 `/proc/meminfo` 文件,帮助用户准确监控内存状态,确保系统稳定运行。
1081 6
|
3月前
|
Linux
在 Linux 系统中,“cd”命令用于切换当前工作目录
在 Linux 系统中,“cd”命令用于切换当前工作目录。本文详细介绍了“cd”命令的基本用法和常见技巧,包括使用“.”、“..”、“~”、绝对路径和相对路径,以及快速切换到上一次工作目录等。此外,还探讨了高级技巧,如使用通配符、结合其他命令、在脚本中使用,以及实际应用案例,帮助读者提高工作效率。
177 3
|
3月前
|
监控 安全 Linux
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景,包括 ping(测试连通性)、traceroute(跟踪路由路径)、netstat(显示网络连接信息)、nmap(网络扫描)、ifconfig 和 ip(网络接口配置)。掌握这些命令有助于高效诊断和解决网络问题,保障网络稳定运行。
137 2
|
12天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
27 11
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
95 14
Linux 10 个“who”命令示例
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
184 20
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
119 8
|
2月前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
112 7
|
3月前
|
Linux 数据库
Linux中第一次使用locate命令报错?????
在Linux CentOS7系统中,使用`locate`命令时出现“command not found”错误,原因是缺少`mlocate`包。解决方法是通过`yum install mlocate -y`或`apt-get install mlocate`安装该包,并执行`updatedb`更新数据库以解决后续的“can not stat”错误。
58 9