标签搜索

目 录CONTENT

文章目录

Linux中关于dd命令的使用及说明

沙漠渔
2023-06-14 14:57:26 / 0 评论 / 0 点赞 / 295 阅读 / 3,552 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-06-14,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

dd命令是Linux下的一个非常有用的命令,可以指定大小的块拷贝文件并在拷贝的同时进行各种指定的转换。

使用举例

测试写

dd if=/dev/zero of=/infokist/test/data1 bs=1M count=50000

/dev/zero 是一个输入设备,可以无穷尽的提供0,用来向设备或者文件中写入字符串0

dd if=/dev/zero of=/infokist/test/data2 bs=1M count=100000 oflag=direct

oflag=direct表明直接调用I/O接口进行磁盘读写,不通过系统缓存,相对上面的方式,可以更准确的测出磁盘的写入速度。

测试读

测试磁盘读能力

time dd if=/dev/sdb of=/dev/null bs=4k

/dev/null 是空设备,也成为位桶,外号无底洞,任何写入它的输出都会被抛弃,如果不想让消息以标准输出显示或者写入文件,就可以将消息重定向到/dev/null

同样的,可以通过直接打开direct开关,不通过系统缓存,更准确的测出磁盘的读取速度

time dd if=/dev/sdb of=/dev/null bs=4k oflag=direct

注:bs是一次IO读写的规模,count是读多少个bs,也可以写成count=16G

命令说明

名称: dd
使用权限: 所有使用者
使用方法:dd [操作] ...或者 dd 选项
功能说明:复制文件,根据指定操作进行转换和格式化。

  bs=BYTES        read and write up to BYTES bytes at a time (default: 512);
                  overrides ibs and 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 up to 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=N          skip N obs-sized blocks at start of output
  skip=N          skip N ibs-sized blocks at start of input
  status=LEVEL    The LEVEL of information to print to stderr;
                  'none' suppresses everything but error messages,
                  'noxfer' suppresses the final transfer statistics,
                  'progress' shows periodic transfer statistics
N and BYTES may be followed by the following multiplicative suffixes:
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.

CONV参数如下:

  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
  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
  sync      pad every input block with NULs to ibs-size; when used
            with block or unblock, pad with spaces rather than NULs
  excl          fail if the output file already exists
  nocreat     do not create the output file
  notrunc     不截断输出文件
  noerror     读取数据发生错误后仍然继续
  fdatasync     结束前将输出文件数据写入磁盘
  fsync     类似上面,但是元数据也一同写入

FLAG参数如下:

FLAG 符号可以是:

  append     追加模式(仅对输出有意义;隐含了conv=notrunc)
  direct     使用直接I/O 存取模式
  directory     除非是目录,否则 directory 失败
  dsync          使用同步I/O 存取模式
  sync          与上者类似,但同时也对元数据生效
  fullblock     为输入积累完整块(仅iflag)
  nonblock     使用无阻塞I/O 存取模式
  noatime     不更新存取时间
  nocache   Request to drop cache.  See also oflag=sync
  noctty     不根据文件指派控制终端
  nofollow     不跟随链接文件
  count_bytes  treat 'count=N' as a byte count (iflag only)
  skip_bytes  treat 'skip=N' as a byte count (iflag only)
  seek_bytes  treat 'seek=N' as a byte count (oflag only)

dd命令执行过程中可以通过两种方式获取执行进度,一种是前面已经说过的,status参数,这里就不细说了,在dd命令的说明里还有一个方法,使用如下:

Sending a USR1 signal to a running 'dd' process makes it
print I/O statistics to standard error and then resume copying.

即周期性的发送USR1信号获取dd进度,自行处理即可。

0
广告 广告

评论区