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,如需转载请自行联系原作者



相关文章
|
9月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
979 1
二、Linux文本处理与文件操作核心命令
|
9月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
567 137
|
9月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1505 58
|
12月前
|
JSON 自然语言处理 Linux
linux命令—tree
tree是一款强大的Linux命令行工具,用于以树状结构递归展示目录和文件,直观呈现层级关系。支持多种功能,如过滤、排序、权限显示及格式化输出等。安装方法因系统而异常用场景包括:基础用法(显示当前或指定目录结构)、核心参数应用(如层级控制-L、隐藏文件显示-a、完整路径输出-f)以及进阶操作(如磁盘空间分析--du、结合grep过滤内容、生成JSON格式列表-J等)。此外,还可生成网站目录结构图并导出为HTML文件。注意事项:使用Tab键补全路径避免错误;超大目录建议限制遍历层数;脚本中推荐禁用统计信息以优化性能。更多详情可查阅手册mantree。
1052 143
linux命令—tree
|
8月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1354 2
|
9月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
739 16
|
9月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
843 0
Linux内存问题排查命令详解
|
11月前
|
监控 Linux 网络安全
Linux命令大全:从入门到精通
日常使用的linux命令整理
1674 13
|
12月前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
1958 21
|
12月前
|
监控 Linux
Linux系统中使用df命令详解磁盘使用情况。
`df`命令是Linux系统管理员和用户监控和管理磁盘空间使用的重要工具。掌握它的基本使用方法和选项可以帮助在必要时分析和解决空间相关问题。简洁但功能丰富,`df`命令确保了用户可以快速有效地识别和管理文件系统的空间使用情况。
853 13