国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home System Tutorial LINUX How to Use dd Command in Linux [15 Useful Examples]

How to Use dd Command in Linux [15 Useful Examples]

Jun 25, 2025 am 09:25 AM

Brief: In this advanced guide, we will discuss some practical examples of the dd command. After following this guide, advanced users will be able to work with the block devices comfortably from the command line interface.

In Linux, everything is a file and the block devices are not an exception to it. Often time, 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 Master Boot Record (MBR), making a bootable USB drive, and the list goes on.

Certainly, we can use graphical tools to perform all these operations. However, most Linux administrators prefer to use the dd command due to its rich functionality and robustness.

In this advanced guide, we will learn about the dd command to convert and copy files. However, unlike the cp command most of the time it’s used with block devices.

In this guide, first, we will understand the usage of the dd command with basic examples then we will discuss some advanced use cases.

Table of Contents

So let’s get started.

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.
Precaution – It is important to note that the dd command must be used very cautiously while working with the block devices. A small mistake can cause permanent data loss. Hence it is strongly commended to try out these operations on the test machine first.

1. How to Copy a File in Linux

One of the basic use of the dd command is to copy a file into a 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

In this example, the if parameter represents the source file whereas the of parameter represents the destination file.

How to Use dd Command in Linux [15 Useful Examples]

Isn’t it exactly similar to the cp command? Then what’s so special about the dd command?

The dd command is much more powerful than the regular cp command. The latter sections of the tutorial discuss some of its advanced use cases.

2. How to Convert Text from Lowercase to Uppercase

The dd command allows us to perform case conversion. To achieve this we can use the conv parameter with it.

To understand this, first, display the contents of the file-1.txt file:

<strong>$ cat file-1.txt</strong>

this is a sample text file

Now, let’s convert the file contents to the upper case using the following command:

$ dd if=file-1.txt of=upper-case.txt conv=ucase

In this example, the conv=ucase option is used to convert the lowercase letters to uppercase letters.

Finally, verify the contents of the newly created file:

<strong>$ cat upper-case.txt</strong>

THIS IS A SAMPLE TEXT FILE

How to Use dd Command in Linux [15 Useful Examples]

3. How to Convert Text from Uppercase to Lowercase

In a similar way, we can use the dd command to convert the upper case letters into lower case letters:

Let’s use the conv=lcase option to convert the upper case letters to lower case:

$ dd if=upper-case.txt of=lower-case.txt conv=lcase

Now, let’s display the contents of the newly created file and verify that the conversion has been done correctly:

$ cat lower-case.txt

<strong>this is a sample text file</strong>

How to Use dd Command in Linux [15 Useful Examples]

4. Avoid Overwriting Destination File in Linux

By default, the dd command replaces the destination file, which means it will overwrite the file if it exists at the destination with the same name.

However, we can disable this default behavior using the conv=excl option as shown.

$ dd if=file-1.txt of=file-2.txt conv=excl

<strong>dd: failed to open ‘file-2.txt’ File exists</strong>

Here, we can see that the dd command has aborted the operations because the file with the same name is present at the destination.

5. Append Data in a File Using dd Command

Sometimes, we want to update the file in an append mode, which means the new contents should be added to the end of the destination file.

We can achieve this by combining the two flags – oflag=append and conv=notrunc. Here, the oflag represents the output flag whereas the notrunc option is used to disable the truncation at the destination.

To understand this, first, let’s create a new text file:

$ echo "append example demo" > dest.txt

Next, let’s append the contents to the dest.txt file using the following command:

$ dd if=file-1.txt of=dest.txt oflag=append conv=notrunc

Now, let’s check the contents of the dest.txt file:

$ cat dest.txt 

append example demo
this is a sample text file

How to Use dd Command in Linux [15 Useful Examples]

6. Skip Bytes or Characters While Reading the Input File

We can instruct the dd command to skip the first few characters while reading the input file using the ibs and skip options.

First, let’s display the contents of the file-1.txt file:

$ cat file-1.txt

this is a sample text file

Next, let’s skip the first 8 characters by using the following command:

$ dd if=file-1.txt of=file-2.txt ibs=8 skip=1

Now, let’s verify the contents of the file-2.txt file:

$ cat file-2.txt

a sample text file

How to Use dd Command in Linux [15 Useful Examples]

In the above output, we can see that the command has skipped the first 8 characters.

7. Backup Linux Disk Partition Using dd Command

So far we discussed the basic examples of the dd command that doesn’t require root access. Now, let’s see some advanced use cases.

Just like files, we can take the backup of the disk partition using the dd command. For example, the below command takes a backup of the /dev/sda1 partition to partition-bkp.img:

$ sudo dd if=/dev/sda1 of=partition-bkp.img

How to Use dd Command in Linux [15 Useful Examples]

8. Restore Linux Disk Partition Using dd Command

In the previous example, we backed up the /dev/sda1 partition to the partition-bkp.img file.

Now, let’s restore it to the /dev/sdb1 partition using the following command:

$ sudo dd if=partition-bkp.img of=/dev/sdb1

How to Use dd Command in Linux [15 Useful Examples]

It is important to note that, the size of the destination partition must be equal to or larger than the backup size.

9. Backup Entire Linux Hard Drive Using dd Command

The disk drive can have multiple partitions. So taking and restoring backup per partition can become time-consuming as the number of partitions increases. To overcome this limitation, we can back up the entire disk drive just like the partitions.

So, let’s take the backup of the /dev/sda disk using the following command:

$ sudo dd if=/dev/sda of=disk-bkp.img

How to Use dd Command in Linux [15 Useful Examples]

The above command takes back up the entire disk including its partitions.

10. Restore the Linux Hard Drive Using dd Command

Just like partitions, we can restore the backup of the entire disk. In the previous example, we backed up the entire disk to the disk-bkp.img file. Now, let’s use the same to restore it on the /dev/sdb disk.

First, let’s delete all the partitions from the /dev/sdb disk and verify that all the partitions have been deleted:

$ lsblk /dev/sdb

Next, let’s restore the backup on the /dev/sdb drive using the following command:

$ sudo dd if=disk-bkp.img of=/dev/sdb

Finally, verify that the partition has been created on the /dev/sdb disk:

$ lsblk /dev/sdb

How to Use dd Command in Linux [15 Useful Examples]

11. Backup Master Boot Record Using dd Command

The Master Boot Record (MBR) is located in the first sector of the boot disk. It stores information about the disk partitions. We can use the dd command as shown below to take a back of it:

$ sudo dd if=/dev/sda of=mbr.img bs=512 count=1

The above command takes back up the first 512 bytes i.e. one sector.

How to Use dd Command in Linux [15 Useful Examples]

It is important to note that, the above command must be executed on the boot disk.

12. Restore Master Boot Record Using dd Command

In the previous example, we backed up the Master Boot Record (MBR). Now, let’s restore it on the /dev/sdb disk using the following command:

$ sudo dd if=mbr.img of=/dev/sdb

How to Use dd Command in Linux [15 Useful Examples]

13. Copy CD/DVD Drive Content Using dd Command

Similar to the partitions and disks, we can use the dd command to copy contents from the CD or DVD drive. So let’s use the below command to do the same:

$ sudo dd if=/dev/cdrom of=alma-minimal.iso

In Linux, the CD/DVD drive is represented by the /dev/cdrom device. Hence we are using it as a source file.

Now, let’s verify that the contents have been copied successfully by verifying its checksum command:

$ sha256sum alma-minimal.is

How to Use dd Command in Linux [15 Useful Examples]

14. Create a Bootable USB Drive Using dd Command

In the previous example, we created an iso image of the Alma Linux. Now let’s use it to create a bootable USB drive:

$ sudo dd if=alma-minimal.iso of=/dev/sdb

How to Use dd Command in Linux [15 Useful Examples]

It is important to note that, the above command must be executed with the correct USB drive.

15. How to Show the Progress Bar

By default, the dd command doesn’t show the progress while doing the copy operation. However, we can override this default behavior using the status option.

So let’s use the status=progress option with the dd command to show the progress bar:

$ sudo dd if=alma-minimal.iso of=/dev/sdb status=progress

How to Use dd Command in Linux [15 Useful Examples]

Conclusion

In this article, we discussed some practical examples of the dd command. Advanced users can refer to these examples in day-to-day life while working with Linux systems. However, we have to be very careful while executing these commands. Because a small mistake can overwrite the contents of the entire disk.

Do you know of any other best example of the dd command in Linux? Let us know your views in the comments below.

The above is the detailed content of How to Use dd Command in Linux [15 Useful Examples]. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to troubleshoot DNS issues on a Linux machine? How to troubleshoot DNS issues on a Linux machine? Jul 07, 2025 am 12:35 AM

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

Install Guacamole for Remote Linux/Windows Access in Ubuntu Install Guacamole for Remote Linux/Windows Access in Ubuntu Jul 08, 2025 am 09:58 AM

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

How to find my private and public IP address in Linux? How to find my private and public IP address in Linux? Jul 09, 2025 am 12:37 AM

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

How to Install NodeJS 14 / 16 & NPM on Rocky Linux 8 How to Install NodeJS 14 / 16 & NPM on Rocky Linux 8 Jul 13, 2025 am 09:09 AM

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

System requirements to install linux System requirements to install linux Jul 20, 2025 am 03:49 AM

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

How to Install MySQL 8.0 on Rocky Linux and AlmaLinux How to Install MySQL 8.0 on Rocky Linux and AlmaLinux Jul 12, 2025 am 09:21 AM

Written in C, MySQL is an open-source, cross-platform, and one of the most widely used Relational Database Management Systems (RDMS). It’s an integral part of the LAMP stack and is a popular database management system in web hosting, data analytics,

Ubuntu 25.04 'Plucky Puffin”: A Bold Leap Forward with GNOME 48 and HDR Brilliance Ubuntu 25.04 'Plucky Puffin”: A Bold Leap Forward with GNOME 48 and HDR Brilliance Jul 12, 2025 am 09:28 AM

Ubuntu has long stood as a bastion of accessibility, polish, and power in the Linux ecosystem. With the arrival of Ubuntu 25.04, codenamed “Plucky Puffin”, Canonical has once again demonstrated its commitment to delivering a

How to Install MongoDB on Rocky Linux and AlmaLinux How to Install MongoDB on Rocky Linux and AlmaLinux Jul 12, 2025 am 09:29 AM

MongoDB is a high-performance, highly scalable document-oriented NoSQL database built to manage heavy traffic and vast amounts of data. Unlike traditional SQL databases that store data in rows and columns within tables, MongoDB structures data in a J

See all articles