The dd command is an extremely powerful Linux utility. It is commonly referred to as the "disk destroyer", “data definition”, “disk dump”, or "disk duplicator" due to its immense power and ability to directly interact with block devices. In this beginner's guide to the dd command in Linux, we will explore its syntax and various use cases, highlighting its role in file copying, disk partition backup, and restoration, and creating bootable USB drives.
The syntax of the dd command is simple. It reads from standard input and writes to standard output by default. Here is the basic syntax of the dd command:
$ dd [OPTION]...
It's worth noting that dd deviates from the standard convention of using the -- or - syntax for options, distinguishing it from most Linux shell commands.
The dd command accepts several options to customize its behavior and achieve specific tasks. Here are some of the most commonly used options:
| Option | Description |
|---|---|
if |
Specifies the input file (source). |
of |
Specifies the output file (destination). |
bs |
Defines the block size to read from the input file and write to the output file. |
count |
Specifies the number of blocks to copy. |
skip |
Skips a specific number of blocks or bytes while reading the input file. |
seek |
Skips a specific number of blocks or bytes while writing to the output file. |
status |
Shows the progress of the dd command. |
conv |
Specifies conversion options for the input or output file. |
Now, let's explore some practical examples of using the dd command in various scenarios.
To make a simple copy of a file, you can use the dd command with the if and of options. For example, to copy a file named source.txt to a new file named destination.txt, run the following command:
$ dd if=source.txt of=destination.txt
This command reads the contents of source.txt and writes them to destination.txt.
When using the dd command, if there is already a file with the same name at the destination, it will be replaced by default. This means that the existing file will be overwritten. Use the conv=notrunc option to prevent overwriting an existing file while using the dd command. This option ensures that the destination file is not truncated during the write process. For example:
$ dd if=source.txt of=destination.txt conv=notrunc
In addition to avoiding overwriting, you can also append data to an existing file by using the conv=append option. Let's consider an example where we want to append the contents of users.txt to newusers.txt: