dd Command Cheatsheet - Linux File Operations & Benchmarking

Master the dd command with this comprehensive cheatsheet. Learn to create files, benchmark disk I/O, test write/read speeds, and optimize Linux file operations.

dd Command Cheatsheet

Linux File Operations with dd

The dd command is a powerful utility in Linux for low-level copying and conversion of data. It's widely used for tasks ranging from creating disk images to benchmarking storage performance. This cheatsheet covers essential dd commands for file operations and performance testing.

Create Files

To create a file of a specific size, such as a 1GB file, you can use dd with /dev/zero as the input source. This is useful for allocating space or creating test files.

$ dd if=/dev/zero of=1g.bin bs=1G count=1

This command creates a file named 1g.bin with a block size of 1 Gigabyte and copies 1 block, resulting in a 1GB file.

Benchmarking Disk I/O

dd is an excellent tool for measuring the performance of your storage devices. You can test both sequential and random I/O operations.

Server Throughput (Streaming I/O)

To test sequential write throughput, you can write a large block of zeros to a file and use the dsync flag to ensure data is physically written to disk.

$ dd if=/dev/zero of=/root/testfile bs=1G count=1 oflag=dsync

Server Latency

Testing latency involves writing smaller blocks and measuring the time it takes. This command writes 1000 blocks of 512 bytes each.

$ dd if=/dev/zero of=/root/testfile bs=512 count=1000 oflag=dsync

Testing Write Speed

To accurately test the write speed of your storage, you can copy data from /dev/urandom (which generates random data) to a file. Setting a larger block size (e.g., 1MB) can provide more stable results for sequential writes.

# Set the block size to 1MB and copy 1000 blocks
dd if=/dev/urandom of=testfile bs=1M count=1000

The output will show the data transfer rate, indicating the write speed.

Testing Read Speed

To test read speed, you can copy data from an existing file to /dev/null, which discards all data written to it. This effectively measures how fast the data can be read from the source file.

dd if=testfile of=/dev/null bs=1M

This command reads the testfile with a block size of 1MB and sends it to /dev/null, reporting the read speed.

For more in-depth information on the dd command and its options, refer to the official documentation: