Intro To 'dd' Command In Linux
2023-05-12 - By Robert Elder
I use the 'dd' command to copy large blocks of data.
Copy A File With The 'dd' Command
I can use the 'dd' command to copy any ordinary file. For example, if I start with a file called 'example.txt':
cat example.txt
Hello World!
I can make a copy of the 'example.txt' file using the 'dd' command like this:
dd if=example.txt of=my_copy.txt
and now the file 'my_copy.txt' contains the same piece of information as 'example.txt':
cat my_copy.txt
Hello World!
Imaging A Hard Drive With The 'dd' Command
Even though you can use the 'dd' command to copy regular files, I mostly use it for creating backup images of hard drives. If I want to make a copy of my hard drive that's represented by the block device at '/dev/sdc', I can use this command:
dd if=/dev/sdc of=my-image.img status=progress
The 'if' stands for 'input file' and the 'of' stands for 'output file'.
You can also copy the image file back onto the hard disk to exactly restore our previous state by swapping the order of the input and the output files. But be careful! This use of the 'dd' command is destructive! It will completely destroy all of the data that was previously on the '/dev/sdc' disk:
dd if=my-image.img of=/dev/sdc status=progress
Create A Large File Filled With Zeros Using 'dd'
I can also use the 'dd' command to create large files for testing purposes. For example, this command will create a one megabyte file filled with only zeros:
dd if=/dev/zero of=file-with-zeros bs=1M count=1
And here is a hexadecimal representation of the start of the resulting file:
head -c 128 file-with-zeros | xxd
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
Create A Large File Filled With Random Data Using 'dd'
This command will create a 10 gigabyte file that contains random data:
dd if=/dev/urandom of=random-data.dat bs=1M count=10240 status=progress
And here is a hexadecimal representation of the start of the resulting file:
head -c 128 random-data.dat | xxd
00000000: 2e85 a601 8148 bb0e d0d3 805c d27a cfc5 .....H.....\.z..
00000010: 4463 2f1f 5cd0 3b9c 9cfb 1e9a 2b05 944e Dc/.\.;.....+..N
00000020: a24b 4645 a097 ae6e 3a67 7abc 8516 0baf .KFE...n:gz.....
00000030: 2ff7 9833 b9c2 a4db 1777 b215 e5bd 13fc /..3.....w......
00000040: a14a f9c1 363b 8367 0f05 1ca1 b3e6 f169 .J..6;.g.......i
00000050: 5d98 8fa8 0edb 7eaf 7092 6695 1ffa 74d8 ].....~.p.f...t.
00000060: f5c6 907c ff9d f651 1570 b0d0 8fa3 8ee4 ...|...Q.p......
00000070: 3a9e ea48 5eb0 84f3 16e5 e2d8 96af c809 :..H^...........
And that's why the 'dd' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|