Linux -- dd 命令
11.2 `dd‘: Convert and copy a file
==================================
`dd‘ copies a file (from standard input to standard output, by default)
with a changeable I/O block size, while optionally performing
conversions on it. Synopses:
dd [OPERAND]...
dd OPTION
The only options are `--help‘ and `--version‘. *Note Common
options::. `dd‘ accepts the following operands, whose syntax was
inspired by the DD (data definition) statement of OS/360 JCL.
`if=FILE‘
Read from FILE instead of standard input.
`of=FILE‘
Write to FILE instead of standard output. Unless `conv=notrunc‘
is given, `dd‘ truncates FILE to zero bytes (or the size specified
with `seek=‘).
`ibs=BYTES‘
Set the input block size to BYTES. This makes `dd‘ read BYTES per
block. The default is 512 bytes.
#一次读取bytes字节,即读入缓冲区的字节数。
`obs=BYTES‘
Set the output block size to BYTES. This makes `dd‘ write BYTES
per block. The default is 512 bytes.
#一次写入bytes字节,即写入缓冲区的字节数。
`bs=BYTES‘
Set both input and output block sizes to BYTES. This makes `dd‘
read and write BYTES per block, overriding any `ibs‘ and `obs‘
settings. In addition, if no data-transforming `conv‘ option is
specified, input is copied to the output as soon as it‘s read,
even if it is smaller than the block size.
#同时设置读/写缓冲区的字节数(等于设置ibs和obs)。
`cbs=BYTES‘
Set the conversion block size to BYTES. When converting
variable-length records to fixed-length ones (`conv=block‘) or the
reverse (`conv=unblock‘), use BYTES as the fixed record length.
#一次转换bytes字节。
`skip=N‘
Skip N `ibs‘-byte blocks in the input file before copying. If
`iflag=skip_bytes‘ is specified, N is interpreted as a byte count
rather than a block count.
#跳过读入缓冲区开头的 ibs*blocks 块。
`seek=N‘
Skip N `obs‘-byte blocks in the output file before copying. if
`oflag=seek_bytes‘ is specified, N is interpreted as a byte count
rather than a block count.
#跳过一段以后才输出
`count=N‘
Copy N `ibs‘-byte blocks from the input file, instead of
everything until the end of the file. if `iflag=count_bytes‘ is
specified, N is interpreted as a byte count rather than a block
count. Note if the input may return short reads as could be the
case when reading from a pipe for example, `iflag=fullblock‘ will
ensure that `count=‘ corresponds to complete input blocks rather
than the traditional POSIX specified behavior of counting input
read operations.
#只拷贝输入的blocks块
#dd if=file1 of=file2 count=N bs=SIZE 最经典的组合
`status=WHICH‘
Transfer information is normally output to stderr upon receipt of
the `INFO‘ signal or when `dd‘ exits. Specifying WHICH will
identify which information to suppress.
`noxfer‘
Do not print the transfer rate and volume statistics that
normally make up the last status line.
`none‘
Do not print any informational messages to stderr. Error
messages are output as normal.
`conv=CONVERSION[,CONVERSION]...‘
Convert the file as specified by the CONVERSION argument(s). (No
spaces around any comma(s).)
Conversions:
`ascii‘ #把EBCDIC码转换为ASCIl码。
Convert EBCDIC to ASCII, using the conversion table specified
by POSIX. This provides a 1:1 translation for all 256 bytes.
`ebcdic‘ #把ASCIl码转换为EBCDIC码。
Convert ASCII to EBCDIC. This is the inverse of the `ascii‘
conversion.
`ibm‘ #把ASCIl码转换为alternate EBCDIC码。
Convert ASCII to alternate EBCDIC, using the alternate
conversion table specified by POSIX. This is not a 1:1
translation, but reflects common historical practice for `~‘,
`[‘, and `]‘.
The `ascii‘, `ebcdic‘, and `ibm‘ conversions are mutually
exclusive.
`block‘ #把变动位转换成固定字符。
For each line in the input, output `cbs‘ bytes, replacing the
input newline with a space and padding with spaces as
necessary.
`unblock‘ #把固定位转换成变动位。
Remove any trailing spaces in each `cbs‘-sized input block,
and append a newline.
The `block‘ and `unblock‘ conversions are mutually exclusive.
`lcase‘ #把字母由大写转换为小写。
Change uppercase letters to lowercase.
`ucase‘ #把字母由小写转换为大写。
Change lowercase letters to uppercase.
The `lcase‘ and `ucase‘ conversions are mutually exclusive.
`sparse‘
Try to seek rather than write NUL output blocks. On a file
system that supports sparse files, this will create sparse
output when extending the output file. Be careful when using
this option in conjunction with `conv=notrunc‘ or
`oflag=append‘. With `conv=notrunc‘, existing data in the
output file corresponding to NUL blocks from the input, will
be untouched. With `oflag=append‘ the seeks performed will
be ineffective. Similarly, when the output is a device
rather than a file, NUL input blocks are not copied, and
therefore this option is most useful with virtual or pre
zeroed devices.
`swab‘
Swap every pair of input bytes. GNU `dd‘, unlike others,
works when an odd number of bytes are read--the last byte is
simply copied (since there is nothing to swap it with).
#交换每一对输入字节。
`sync‘
Pad every input block to size of `ibs‘ with trailing zero
bytes. When used with `block‘ or `unblock‘, pad with spaces
instead of zero bytes.
#把每个输入记录的大小都调到ibs的大小(用NUL填充)。
The following "conversions" are really file flags and don‘t affect
internal processing:
`excl‘
Fail if the output file already exists; `dd‘ must create the
output file itself.
`nocreat‘
Do not create the output file; the output file must already
exist.
The `excl‘ and `nocreat‘ conversions are mutually exclusive.
`notrunc‘
Do not truncate the output file.
`noerror‘
Continue after read errors.
`fdatasync‘
Synchronize output data just before finishing. This forces a
physical write of output data.
`fsync‘
Synchronize output data and metadata just before finishing.
This forces a physical write of output data and metadata.
`iflag=FLAG[,FLAG]...‘
Access the input file using the flags specified by the FLAG
argument(s). (No spaces around any comma(s).)
`oflag=FLAG[,FLAG]...‘
Access the output file using the flags specified by the FLAG
argument(s). (No spaces around any comma(s).)
Here are the flags. Not every flag is supported on every operating
system.
`append‘
Write in append mode, so that even if some other process is
writing to this file, every `dd‘ write will append to the
current contents of the file. This flag makes sense only for
output. If you combine this flag with the `of=FILE‘ operand,
you should also specify `conv=notrunc‘ unless you want the
output file to be truncated before being appended to.
`cio‘
Use concurrent I/O mode for data. This mode performs direct
I/O and drops the POSIX requirement to serialize all I/O to
the same file. A file cannot be opened in CIO mode and with
a standard open at the same time.
`direct‘
Use direct I/O for data, avoiding the buffer cache. Note
that the kernel may impose restrictions on read or write
buffer sizes. For example, with an ext4 destination file
system and a linux-based kernel, using `oflag=direct‘ will
cause writes to fail with `EINVAL‘ if the output buffer size
is not a multiple of 512.
`directory‘
Fail unless the file is a directory. Most operating systems
do not allow I/O to a directory, so this flag has limited
utility.
`dsync‘
Use synchronized I/O for data. For the output file, this
forces a physical write of output data on each write. For
the input file, this flag can matter when reading from a
remote file that has been written to synchronously by some
other process. Metadata (e.g., last-access and last-modified
time) is not necessarily synchronized.
`sync‘
Use synchronized I/O for both data and metadata.
`nocache‘
Discard the data cache for a file. When count=0 all cache is
discarded, otherwise the cache is dropped for the processed
portion of the file. Also when count=0 failure to discard
the cache is diagnosed and reflected in the exit status.
Here as some usage examples:
# Advise to drop cache for whole file
dd if=ifile iflag=nocache count=0
# Ensure drop cache for the whole file
dd of=ofile oflag=nocache conv=notrunc,fdatasync count=0
# Drop cache for part of file
dd if=ifile iflag=nocache skip=10 count=10 of=/dev/null
# Stream data using just the read-ahead cache
dd if=ifile of=ofile iflag=nocache oflag=nocache
`nonblock‘
Use non-blocking I/O.
`noatime‘
Do not update the file‘s access time. Some older file
systems silently ignore this flag, so it is a good idea to
test it on your files before relying on it.
`noctty‘
Do not assign the file to be a controlling terminal for `dd‘.
This has no effect when the file is not a terminal. On many
hosts (e.g., GNU/Linux hosts), this option has no effect at
all.
`nofollow‘
Do not follow symbolic links.
`nolinks‘
Fail if the file has multiple hard links.
`binary‘
Use binary I/O. This option has an effect only on nonstandard
platforms that distinguish binary from text I/O.
`text‘
Use text I/O. Like `binary‘, this option has no effect on
standard platforms.
`fullblock‘
Accumulate full blocks from input. The `read‘ system call
may return early if a full block is not available. When that
happens, continue calling `read‘ to fill the remainder of the
block. This flag can be used only with `iflag‘. This flag
is useful with pipes for example as they may return short
reads. In that case, this flag is needed to ensure that a
`count=‘ argument is interpreted as a block count rather than
a count of read operations.
`count_bytes‘
Interpret the `count=‘ operand as a byte count, rather than a
block count, which allows specifying a length that is not a
multiple of the I/O block size. This flag can be used only
with `iflag‘.
`skip_bytes‘
Interpret the `skip=‘ operand as a byte count, rather than a
block count, which allows specifying an offset that is not a
multiple of the I/O block size. This flag can be used only
with `iflag‘.
`seek_bytes‘
Interpret the `seek=‘ operand as a byte count, rather than a
block count, which allows specifying an offset that is not a
multiple of the I/O block size. This flag can be used only
with `oflag‘.
These flags are not supported on all systems, and `dd‘ rejects
attempts to use them when they are not supported. When reading
from standard input or writing to standard output, the `nofollow‘
and `noctty‘ flags should not be specified, and the other flags
(e.g., `nonblock‘) can affect how other processes behave with the
affected file descriptors, even after `dd‘ exits.
The numeric-valued strings above (N and BYTES) can be followed by a
multiplier: `b‘=512, `c‘=1, `w‘=2, `xM‘=M, or any of the standard block
size suffixes like `k‘=1024 (*note Block size::).
Any block size you specify via `bs=‘, `ibs=‘, `obs=‘, `cbs=‘ should
not be too large--values larger than a few megabytes are generally
wasteful or (as in the gigabyte..exabyte case) downright
counterproductive or error-inducing.
To process data that is at an offset or size that is not a multiple
of the I/O block size, you can use the `skip_bytes‘, `seek_bytes‘ and
`count_bytes‘ flags. Alternatively the traditional method of separate
`dd‘ invocations can be used. For example, the following shell
commands copy data in 512 KiB blocks between a disk and a tape, but do
not save or restore a 4 KiB label at the start of the disk:
disk=/dev/rdsk/c0t1d0s2
tape=/dev/rmt/0
# Copy all but the label from disk to tape.
(dd bs=4k skip=1 count=0 && dd bs=512k) <$disk >$tape
# Copy from tape back to disk, but leave the disk label alone.
(dd bs=4k seek=1 count=0 && dd bs=512k) <$tape >$disk
Sending an `INFO‘ signal to a running `dd‘ process makes it print
I/O statistics to standard error and then resume copying. In the
example below, `dd‘ is run in the background to copy 10 million blocks.
The `kill‘ command makes it output intermediate I/O statistics, and
when `dd‘ completes normally or is killed by the `SIGINT‘ signal, it
outputs the final statistics.
$ dd if=/dev/zero of=/dev/null count=10MB & pid=$!
$ kill -s INFO $pid; wait $pid
3385223+0 records in
3385223+0 records out
1733234176 bytes (1.7 GB) copied, 6.42173 seconds, 270 MB/s
10000000+0 records in
10000000+0 records out
5120000000 bytes (5.1 GB) copied, 18.913 seconds, 271 MB/s
On systems lacking the `INFO‘ signal `dd‘ responds to the `USR1‘
signal instead, unless the `POSIXLY_CORRECT‘ environment variable is
set.
An exit status of zero indicates success, and a nonzero value
indicates failure.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。