消防员灭火防护鞋:dd: Convert and copy a file

来源:百度文库 编辑:偶看新闻 时间:2024/04/19 23:37:55
dd copies a file (from standard input to standard output, bydefault) with a changeable I/O block size, while optionally performingconversions on it. Synopses:
     dd [operand]...     dd option

The only options are --help and --version. See Common options. dd accepts the following operands.

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 zerobytes (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.
obs=bytes
Set the output block size to bytes. This makes dd write bytes per block. The default is 512 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.
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.
skip=blocks
Skip blocksibs’-byte blocks in the input file before copying.
seek=blocks
Skip blocksobs’-byte blocks in the output file before copying.
count=blocks
Copy blocksibs’-byte blocks from the input file, insteadof everything until the end of the file.
status=noxfer
Do not print the overall transfer rate and volume statisticsthat normally make up the third status line when dd exits.
conv=conversion[,conversion]...
Convert the file as specified by the conversion argument(s). (No spaces around any comma(s).)

Conversions:

ascii
Convert EBCDIC to ASCII,using the conversion table specified by POSIX. This provides a 1:1 translation for all 256 bytes.
ebcdic
Convert ASCII to EBCDIC. This is the inverse of the ‘ascii’ conversion.
ibm
Convert ASCII to alternate EBCDIC,using the alternate conversion table specified by POSIX. This is not a 1:1 translation, but reflects common historical practicefor ‘~’, ‘[’, and ‘]’.

The ‘ascii’, ‘ebcdic’, and ‘ibm’ conversions aremutually exclusive.

block
For each line in the input, output ‘cbs’ bytes, replacing theinput 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.

swab
Swap every pair of input bytes. gnu dd, unlike others, workswhen 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 ofzero bytes.

The following “conversions” are really file flagsand don't affect internal processing:

excl
Fail if the output file already exists; dd must create theoutput 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 physicalwrite of output data.
fsync
Synchronize output data and metadata just before finishing. Thisforces a physical write of output data and metadata.

iflag=flag[,flag]...
Access the input file using the flags specified by the flagargument(s). (No spaces around any comma(s).)
oflag=flag[,flag]...
Access the output file using the flags specified by the flagargument(s). (No spaces around any comma(s).)

Here are the flags. Not every flag is supported on every operatingsystem.

append
Write in append mode, so that even if some other process is writing tothis file, every dd write will append to the currentcontents 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 theoutput file to be truncated before being appended to.
cio
Use concurrent I/O mode for data. This mode performs direct I/Oand 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 thesame 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 theoutput buffer size is not a multiple of 512.
directory
Fail unless the file is a directory. Most operating systems do notallow I/O to a directory, so this flag has limited utility.
dsync
Use synchronized I/O for data. For the output file, this forces aphysical write of output data on each write. For the input file,this flag can matter when reading from a remote file that has beenwritten 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 processedportion of the file. Also when count=0failure to discard the cache is diagnosedand 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 goodidea 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 effectat 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 nonstandardplatforms that distinguish binary from text I/O.
text
Use text I/O. Like ‘binary’, this option has no effect onstandard platforms.
fullblock
Accumulate full blocks from input. The read system callmay return early if a full block is not available. When that happens, continue calling read to fill the remainderof the block. This flag can be used only with iflag.

These flags are not supported on all systems, and ‘dd’ rejectsattempts to use them when they are not supported. When reading fromstandard 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 theaffected file descriptors, even after dd exits.

The numeric-valued strings above (bytes and blocks) can befollowed by a multiplier: ‘b’=512, ‘c’=1,‘w’=2, ‘xm’=m, or any of thestandard block size suffixes like ‘k’=1024 (see Block size).

Any block size you specify via ‘bs=’, ‘ibs=’, ‘obs=’, ‘cbs=’should not be too large—values larger than a few megabytesare generally wasteful or (as in the gigabyte..exabyte case) downrightcounterproductive or error-inducing.

Use different dd invocations to use different block sizes forskipping and I/O. For example, the following shell commands copy datain 512 KiB blocks between a disk and a tape, but do not save or restore a4 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 ddprocess makes it print I/O statistics to standard errorand 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 theSIGINT 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_CORRECTenvironment variable is set.

An exit status of zero indicates success,and a nonzero value indicates failure.