Lesson 23: dd Command
In this lesson, you'll learn how to use the dd command to copy files, perform conversions, and manage block devices in Linux.
In Linux, everything is a file, and block devices are not an exception to this. Oftentimes, we need to work with block devices.
As Linux users, we perform a variety of operations on block devices, such as taking a backup of a disk or partition, backing up the Master Boot Record (MBR), making a bootable USB drive, and much more.
While graphical tools are available for performing these tasks, many Linux administrators lean towards using the dd command.
The dd command is favored for its extensive capabilities and reliability, making it a preferred choice among Linux professionals.
Note: The dd command is primarily employed for file conversion and copying, but it distinguishes itself from the cp command by its frequent utilization with block devices.
Let's understand the usage of the dd command with some examples.
dd Command Syntax
The most common syntax of the dd command is as follows:
$ dd [if=] [of=]
In the above syntax:
if- represents the input or source file.of- represents the output or destination file.
dd Command Options
| Option | Description |
|---|---|
if=FILE |
Input file or device (source) |
of=FILE |
Output file or device (destination) |
bs=BYTES |
Set input and output block size |
count=N |
Copy only N input blocks |
skip=N |
Skip N input blocks before copying |
conv=ucase |
Convert text to uppercase |
conv=lcase |
Convert text to lowercase |
conv=excl |
Abort if the output file already exists |
conv=notrunc |
Disable truncation at the destination |
oflag=append |
Append data to the output file |
status=progress |
Show transfer progress |
Precaution: It is important to note that the dd command must be used very cautiously while working with block devices. A small mistake can cause permanent data loss. Hence it is strongly recommended to try out these operations on a test machine first.
1. Copy a File
One of the basic uses of the dd command is to copy a file into the current directory. Let's understand by creating a simple text file:
$ echo "this is a sample text file" > file-1.txt
Now, let's create a copy of it using the dd command:
$ dd if=file-1.txt of=file-2.txt
0+1 records in
0+1 records out
27 bytes copied, 0.000212 s, 127 kB/s
In this example, the "if" parameter represents the source file,, whereas the "of" parameter represents the destination file.
The dd command is much more powerful than the regular cp command. You will witness it in the upcoming dd command use cases.