Linux dd Command

29/12/2020
“dd” is a linux CLI tool. Now, the current version of “dd” is a part of the GNU coreutils, is the one that every single UNIX/Linux system features. The primary purpose of this tool is to convert and copy files. However, it’s quite different than other file copying tools like “cp”.

dd features

“dd” can be used for various purposes:

  • Using “dd”, it’s possible to directly read and/or write from/to different files provided that the function is already implemented in the respected drivers.
  • It’s super useful for purposes like backing up the boot sector, obtaining random data etc.
  • Data conversion, for example, converting ASCII to EBCDIC encoding.

dd usage

Here are some of the most common and interesting usage of “dd”. Of course, “dd” is far more capable than these things. If you’re interested, I always recommend checking out other in-depth resources on “dd”.

Location

which dd

As the output indicates, whenever running “dd”, it launches from “/usr/bin/dd”.

Basic usage

Here’s the structure that “dd” follows.

dd if=<source> of=<destination> <options>

For example, let’s create a file with random data. There are some built-in special files in Linux that appear as normal files like “/dev/zero” that produces a continuous stream of NULL, “/dev/random” that produces continuous random data.

dd if=/dev/urandom of=~/Desktop/random.txt bs=1M count=5

 

The very first options are self-explanatory. It means to use “/dev/urandom” as a source of data and “~/Desktop/random.txt” as the destination. What are the other options?

Here, “bs” stands for “block size”. When dd is writing data, it writes in blocks. Using this option, the block size can be defined. In this case, the value “1M” says that the block size is 1 megabyte.

“count” decides the number of blocks to be written. If not fixed, “dd” will continue the writing process unless the input stream ends. In this case, “/dev/urandom” will continue generating data infinitely, so this option was paramount in this example.

Data backup

Using this method, “dd” can be used to dump the data of an entire drive! All you need is tell the drive as the source.

dd if=<source> of=<backup_location>

If you’re going for such actions, make sure that your source isn’t a directory. “dd” has no idea how to process a directory, so things won’t work.

“dd” only knows how to work with files. So, if you’re in need of backing up a directory, use tar to archive it first, then use “dd” to transfer it on a file.

tar cvJf demo.tar.xz DemoDir/

dd if=demo.tar.xz of=~/Desktop/backup.img

 

In the next example, we’ll be performing a very sensitive operation: backing up the MBR! Now, if your system is using MBR (Master Boot Record), then it’s located at the first 512 bytes of the system disk: 466 bytes for the bootloader, others for the partition table.

Run this command for backing up the MBR record.

dd if=/dev/sda of=~/Desktop/mbr.img bs=512 count=1

Data restore

For any backup, the way of restoring the data is necessary. In the case of “dd”, the restoring process is a bit different than any other tools. You have to re-write the backup file on a similar folder/partition/device.

For example, I have this “backup.img” file containing the “demo.tar.xz” file. To extract it, I used the following command.

dd if=backup.img of=demo.tar.xz

Again, make sure that you’re writing the output to a file. “dd” is no good with directories, remember?

Similarly, if “dd” was used to create a backup of a partition, then restoring it would require the following command.

dd if=<backup_file> of=<target_device>

For example, how about restoring the MBR we backed up earlier?

dd if=mbr.img of=/dev/sda

“dd” options

At some point in this guide, you faced some “dd” options like “bs” and “count”, right? Well, there are more of them. Here’s a shortlist on what they are and how to use them.

  • obs: Determines the size of data to be written at a time. The default value is 512 bytes.

  • cbs: Determines the size of data to be converted at a time.

  • ibs: Determines the size of data to be read at a time.
  • count: Copy only N blocks

  • seek: Skip N blocks at the start of the output

  • skip: Skip N blocks at the start of the input

conv=ascii: Converts the file input from EBCDIC to ASCII

conv=ebcdic: Converts the file input from ASCII to EBCDIC

conv=ibm: converts the file input from ASCII to alternate EBCDIC

conv=lcase: converts file input from Uppercase to lowercase

conv=ucase: converts file input from Lowercase to uppercase

conv=swab: Swap every input pair

Additional options:

  • nocreat: Don’t create the output file
  • notruc: Don’t truncate the output file
  • noerror: Continue the operation, even after facing error
  • fdatasync: Write data to the physical storage before the process finishes
  • fsync: Similar to fdatasync, but also writes the metadata
  • iflag: Tweak the operation based on various flags. Available flags include: append to Append data to the output

Additional options:

  • directory: Facing a directory will fail the operation
  • dsync: Synchronized I/O for data
  • sync: Similar to dsync but includes metadata
  • nocache: Requests for dropping cache.
  • nofollow: Don’t follow any symlink

Additional Options:

  • count_bytes: Similar to “count=N”
  • seek_bytes: Similar to “seek=N”
  • skip_bytes: Similar to “skip=N”

As you’ve seen, it’s possible to stack multiple flags and options in a single “dd” command to tweak the operation behavior.

dd if=demo.txt of=demo1.txt bs=10 count=100 conv=ebcdic
iflag=append,nocache,nofollow,sync

Final thoughts

The workflow of “dd” is pretty simple. However, for “dd” to truly shine, it’s up to you. There are tons of ways creative ways “dd” can be used to perform clever interactions.

For in-depth information about “dd” and all its options, consult the man and info page.

man dd
ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Best Open Source Games in 2020

There’s no reason to spend $60 on a new AAA release just to bring some variety into your gaming diet when there are many...
29/12/2020

[BigBlueButton][HDSD-2]-Các thao tác của người điều hành với phòng họp (MeetingRoom) trên BigBlueButton

Trong bài viết trước, Onet đã hướng dẫn các bạn cách tạo phòng họp trực tuyến trên BBB. Hôm...
30/12/2020

Install Android on Raspberry Pi 3

Android in its early days ran great on low spec phones and tablets. Raspberry Pi 3 devices today are much more powerful...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024