- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 347字
- 2021-07-09 19:46:26
How to do it...
The easiest way to create a large file of a given size is with the dd command. The dd command clones the given input and writes an exact copy to the output. Input can be stdin, a device file, a regular file, and so on. Output can be stdout, a device file, a regular file, and so on. An example of the dd command is as follows:
$ dd if=/dev/zero of=junk.data bs=1M count=1 1+0 records in 1+0 records out 1048576 bytes (1.0 MB) copied, 0.00767266 s, 137 MB/s
This command creates a file called junk.data containing exactly 1 MB of zeros.
Let's go through the parameters:
- if defines the input file
- of defines the output file
- bs defines bytes in a block
- count defines the number of blocks to be copied
In the previous example, we created a 1 MB file, by specifying bs as 1 MB with a count of 1. If bs was set to 2M and count to 2, the total file size would be 4 MB.
We can use various units for blocksize (bs). Append any of the following characters to the number to specify the size:
We can generate a file of any size using bs. Instead of MB we can use any other unit notations, such as the ones mentioned in the previous table.
/dev/zero is a character special device, which returns the zero byte (\0).
If the input parameter (if) is not specified, dd will read input from stdin. If the output parameter (of) is not specified, dd will use stdout.
The dd command can be used to measure the speed of memory operations by transferring a large quantity of data to /dev/null and checking the command output (for example, 1048576 bytes (1.0 MB) copied, 0.00767266 s, 137 MB/s, as seen in the previous example).