Using the dd command

The dd (Dataset Definition) command is a simple, useful, and surprisingly easy to use tool; With this tool you can do the same, on devices: disks and partitions, as with commercial programs like Norton Ghost, on Windows or free programs like CloneZilla, on Linux, with just a short command line.

Basic Syntax:

The most basic syntax for using the dd command would be this:

sudo dd if=origen of=destino

Where if means "input file = input file“, That is, what you want to copy and of means "output file = output file“, That is, the destination file (where the data will be copied);origin and The destination can be devices (CD or DVD reader, hard disk, floppy disk, pendrive, partition, etc.), backup file or disk image, etc., but not folders or subfolders.

For the smooth use of this command, the first thing is always to be clear about what partitions / hard drives are called in Linux (/ dev / sda1 for example; / dev derives from device = device, in English). to find out the source and destination disk / partition, something we can easily find out with the command sudo fdisk -lo with some graphical partition program like gparted. All the information about the dd command can be consulted with the command man dd and info dd,

This command should be used with caution, and always checking the order and name of the disks / partitions, because the same as a disk is cloned, it deletes it in a jiffy.

Syntax with the pv command: Using the dd command with the previous syntax has a small drawback, since it is a very reserved command - it does not give information - because when it is executed, the terminal prompt remains immobile, so we do not know what is what is happening and how much time is left for it to finish executing. This small inconvenience can be solved by adding the command pv, (*) - which acts as a terminal pipe that measures the data that passes through it - to the syntax of the dd command, so that now the syntax would be:

dd if=origen |pv|dd of=destino

As a result, we would obtain a kind of progress bar in the terminal, the information on bytes transferred, the time it has been running and the transfer rate, all in real time.

(alf) - (~) └──┤ dd if = / devmmcblk0p1 | pv | dd of = / dev / mmcblk0p2 1,630GB 0:21:30 [1,12MB / s] [

Also when finished it shows us statistics of the transfer rate, the total bytes transferred and the total time it took to transfer all the data.

(alf) - (~) └──┤ dd if = / devmmcblk0p1 | pv | dd of = / dev / mmcblk0p2 10530816 + 0 records read <=> 10530816 + 0 records written 5391777792 bytes (5.4 GB) copied, 3873,48 s, 1,4 MB / s 5,02GB 1:04:33 [1,33MB / s] [<=> 10530816 + 0 records read <=> 10530816 + 0 records written 5391777792 bytes (5.4GB) copied, 3873,48, 1,4 s, XNUMX MB / s (alf) - (~) └──┤

(*) Check before executing any command line with this second syntax, that we have the pv package installed on the system, or install it if we don't have it: both with Synaptic.

Let's see some practical examples and options of this command (in its version with the trick explained above with the pv command):

A) About hard drives and partitions:

= Clone a hard drive:

With this we would clone the hda disk in hdb. (IDE disks):

sudo dd if=/dev/hda |pv|dd of=/dev/hdb bs=1M

With this we would clone the sda ​​disk in sdb. (SATA disks):

sudo dd if=/dev/sda |pv|dd of=/dev/sdb bs=1M

With the option bs = 1M, it is achieved that both reading and writing are done in 1 megabyte blocks, (less, it would be slower but more secure, and with more we risk losing data along the way).

Keep in mind that this way the disk is recorded "as is", MBR, partition table, empty space, etc ..., so you can only record on a disk of the same or larger size.

= Write only the first partition (hda1) of the source disk to the destination disk (hdb):

sudo dd if=/dev/hda1 |pv|dd of=/dev/hdb bs=1M

= Burn the entire disk (hda) to the first partition (hdb1) of the destination disk:

sudo dd if=/dev/hda |pv|dd of=/dev/hdb1 bs=1M

= Create an image - can be bin or iso - from the hard disk (hda), in the / home directory:

sudo dd if=/dev/hda |pv|dd of=/home/hda.bin

= Completely erase information on a disc: to do this, fill the disc with random characters, five times. There will be no trace of information on the disk:

for n in {1..5}; do dd if=/dev/urandom |pv|dd of=/dev/hda bs=8b conv=notrunc;

= Erase any partition and entire disk from any device:

sudo dd if=/dev/zero |pv|dd of=/dev/sdx (Full disk erase)

sudo dd if=/dev/zero |pv|dd of=/dev/sdxa (Disk partition erase)

where: x is the disk to erase, a is the partition to erase

This operation is very useful to completely erase any partition, in addition the data that will be erased cannot be recovered, therefore it is a low-level and very safe erasure, very useful for example when the pendrives get infected with viruses with our dear windows.

B) On a CD / DVD

= Create / Mount an .iso image from a CD (or DVD)

To create the .iso image of a CD in the / home directory:

sudo dd if=/dev/cdrom |pv|dd of=/home/imagendeCD.iso

To mount the .iso image from the CD:

sudo mount -o loop imagedeCD.iso /mnt/home

= Recover a scratched DVD (This does not recover the entire DVD, in this case only the readable sectors). It is also suitable for defective hard drives:

sudo dd if=/dev/cdrom |pv|dd of=/home/dvd_recuperado.iso conv=noerror,sync

The noerror option is used to avoid reading errors in any situation.

C) About MBR and VBS:

= Copy / Restore Master Boot Record (MBR):

To copy the MBR:

sudo dd if=/dev/hda |pv|dd of=mbr count=1 bs=512

To restore the MBR:

sudo dd if=mbr |pv|dd of=/dev/hda

= Clean our MBR and partition table:

sudo dd if=/dev/zero |pv|dd of=/dev/hda bs=512 count=1

= Clears the MBR but does not touch the partition table, (very useful to erase the GRUB without losing data on the partitions):

sudo dd if=/dev/zero |pv|dd of=/dev/hda bs=446 count=1

= Copy / Restore Volume Boot Sector (VBS):

To copy the VBS:

sudo dd if=/dev/hda |pv|dd of=/home/sector_arranque_hda count=1 bs=512

To restore the VBS:

sudo dd if=/home/sector_arranque_hda |pv|dd of=/dev/hda

D) Others:

= Burn a disk image to our / home directory, skipping disk errors (very useful for disks that are dying):

sudo dd conv=noerror if=/dev/hda |pv|dd of=~/home/imagen_disco_con_errores.iso

= Create an empty 1 Mb file:

sudo dd if=/dev/zero |pv|dd of=archivo_nuevo_vacio bs=1024 count=1024

= Create a 2Gb swap file:

sudo dd if=/dev/zero |pv|dd of=/swapspace bs=4k count=2048M
mkswap /swapspace
swapon /swapspace

= Convert all letters to uppercase:

sudo dd if=miarchivo |pv|dd of=miarchivo conv=ucase

Use the dd command with a graphical environment program: Gdiskdump, is a graphical environment for this dd command that facilitates the task of clone partitions or disks, quickly and easily. It can be downloaded from the page https://launchpad.net/gdiskdump/ Once we open it, with root permissions - sudo gdiskdump -, we see that the program is very easy to use, since we only have to tell it the partition or disk to clone (Input Format) and its destination (Output Format).


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   sieg84 said

    I almost got a diabetic misuse of the dd command.
    by the way, what would be the difference with dd_rescue?

    1.    ever said

      ddrescue by default does not stop on read errors and tries by various means to recover unreadable information.

    2.    sheko said

      hahahha I also used the dd command very badly, it is much more powerful than I thought.

  2.   rots87 said

    I comment that I believe that every arch user at some point used

  3.   Alf said

    @ sieg84, the difference is that the dd was interrupted with some reading or device error, in this case this will not happen since the damaged block will read it a certain number of times and continue with the next one, it also provides the advantage that We can execute it several times on the same image and it will not start from scratch but will try to complete it.

    It is worth mentioning that dd_rescue I have never used it.

    1.    merlin the debianite said

      If there is any GUI made in qt for KDE that uses this command? or do I have to resign.?

      1.    July said

        Gdiskdump is more than a GUI for this command (it is based on it) and although I don't know if they did it in qt (I think not) it almost certainly works for you in KDE

    2.    sieg84 said

      Thanks for the clarification.
      I have only used dd_rescue to copy ISO to USB (mostly because in the openSUSE wiki they had it).
      dd_rescue tu.iso / dev / sdX
      what I like is that it shows a progress bar:
      http://box.jisko.net/i/110db781.png

  4.   creel said

    Alpha:
    I recently saw this same post posted on the Hatteras Blog. It is likely that he does not take this badly but I do not know, at least as a courtesy it would be good to refer to it. So also he wins in web positioning.

    I don't want to create a dilemma with this, but at first glance the article seems 95% identical except for the terminal screenshots that you have adapted. He even mentioned his sources.

  5.   merlin the debianite said

    Interesting I admit that | pv | did not know him.

    Krel I think the source is in ubuntu wiki or arch greetings.

  6.   Alf said

    @krel, the linux thing is this:

    Throughout my journey in this world I have read and collected things, I have a notebook with more than 400 posts that I put in Ubuntu Mexico, which was the community where I contributed the most in my early days, unfortunately things can be repeated, if you review the large amount post on the net, they are almost in the same order, the thing is that most are shorter.

    I can publish another 5 posts in the week and you can find others similar or the same, the reason? Ubuntu has the most forums / blogs on the net.

    By no means do I think there will be a dilemma, and, if the administrators consider deleting it, it would not offend or bother me at all, I abide by the rules and what follows, among my notes 8 that I do not have them in order) I am looking to add something to avoid coincidence.

    regards

    1.    creel said

      It is no mystery to anyone that at most only 10% of the content generated on the web is new. With which the other 90 is duplicated, renewed or rescued content, and I am not saying that this is less important, on the contrary, it is a form of dissemination of knowledge.

      However, where I stop is something else. When you have a blog of this type there is a personal motivation to help and perhaps even receive recognition for it. At the same time I am sure that most of those who write content about Free Software agree with the idea of ​​sharing. However, let's not forget that even the noblest licenses like BSD ask for recognition.

      Also, the references is a way by which some search engines like google improve the positioning of a website. I'm just saying, since the material was used as a minimum of gratitude, I would refer to that work.

      I think it's great that you share, but what do you lose by making a reference? Just think if from another blog with better positioning than this one the contents of this one would be duplicated. desdelinux Without referencing it, in the end they would bury this blog.

      Hatteras published it 10 days ago and unless he has taken it out of your notebook or you are Luis Puente, I can't explain what you tell me, it's that almost everything is textual. Unlikely but not impossible.

      But nothing, that this kind of thing happens. And I don't know, for personal ethics and for collaborating with another colleague I would refer to him. That is up to you, I don't think Hatteras takes this the wrong way either, quite the contrary.

      1.    msx said
  7.   The one before said

    Hmm… I made a dd if = file.iso of = / media / hard disk… by mistake 😛
    any way to recover files from hard drive ???

    1.    elav said

      You can use Testdisk

    2.    merlin the debianite said

      A friend has happened to me perhaps with testdisk or with a specialized live distro to rescue data. My recommendation to make a backup for the next time since using that command is something delicate in my rookie days to format the entire hard disk without wanting to and now to reboot well …… Needless to say, neither the grub started.

  8.   Raul said

    Can someone tell me what command to use to see the copying progress with the dd command, what I mean is to see a progress bar with the% that has been copying.

    Slds

    1.    July said

      Friend! adding the command pv. In this same post he says it almost at the beginning and explains how to use it. Read it again

  9.   pedro said

    using this command:
    dd if = / dev / sdc | bzip2> /media/Elements/iso.gz
    I have managed to make an iso image of a 16Gb disk and save it to a 400Gb disk.
    But I don't know what the command would be to restore that image to the original 16GB disk or to another of the same capacity

  10.   Luis Rodriguez said

    a question

    use this command to
    for n in {1..5}; do dd if = / dev / urandom | pv | dd of = / dev / hda bs = 8b conv = notrunc;
    for a 2TB disk
    then I stay in the console something like that
    >
    Should I run another command or how long can the process take?

    thank you and keep going

    1.    Jazb said

      There is a typo in the for… missing at the end of the line «; done »stayed« for n in {1..5}; do dd if = / dev / urandom | pv | dd of = / dev / hda bs = 8b conv = notrunc; done »

  11.   henry salazar said

    very good article… ..I am going to put it into practice… greetings buddy

  12.   Marcos_tux said

    Very useful, it helped me a lot thanks

  13.   EDFR said

    Good Alf. Thanks for the info.

    A question. Is there a way to copy only part of the source disk? The case is, if I have a 120Gb disk in which only 25Gb is being used and I want to make a copy to a 40Gb disk, I would only be interested in copying the used space (25Gb), is there a way with this command?
    Thanks in advance.

    1.    yukiteru said

      It would be better for you to use rsync, with the command rsync -av / path / to / origin / path / to / destiny you would already be backing up all your permissions with their corresponding permissions.

    2.    yukiteru said

      I meant: "all your files with their corresponding permissions"

  14.   FOXSPY said

    Dear, does anyone know why when occupying the dd command, it reaches only 4.2GB and indicates that the file is very large? It is an image of a 250GB disk and goes to a 1TB external disk.

    sudo dd if = / dev / sda | pv | dd of = / home / fox / backup.iso

    1.    Abductor said

      The 4Gb error is giving it to you because you are trying to make a backup in a partition with fs FAT16

  15.   Bern said

    What a good blog. Very useful entry. I had already seen the uses of this command elsewhere, but they did not explain anything at all. Thank you.

  16.   Carlos Torres said

    hello I have an injection machine in which the program was lost since they replaced the bios stack, we do not have backup but we have another same machine, it only has floppy and serial and parallel port, how can I make a backup of this machine to load it in the other, I hope you can help me thanks

  17.   ernesto said

    I just want to copy a partition without starting from LIVE CD. For example I have a disk with / dev / sda1, / dev / sda5, / dev / sda6 and I want to make a copy to / dev / sda5 from another disk / dev / sdb1

    the command is dd if = / dev / sdb1 of = / dev / sda5 bs = 1M ????

  18.   Pedro Lobato said

    Hello how are you. I also live in Guadalajara.
    Your article on the DD command is very interesting. I want to try some Linux distributions, specifically Archlinux, but I have not been able to use the DD command and copy the Archilinux ISO that I already downloaded, onto a USB stick. I do not know if you can pass me a tick to start with this. Thank you, Pedro lobato

  19.   Victor said

    Because when an iso is recorded with the command "dd" it only works for booting on some PCs?
    Exp. I have 2 laptops and none of them boots the iso of any linux distribution; but on the contrary, it does boot on an old desktop PC with idle disk

    1.    seb said

      The best thing is to play safe using unetbootin, with dd I have also been left with the question of why sometimes it does not work.

      1.    elav said

        I don't think it depends on DD but on ISO. Same thing with Unetbootin, it doesn't work well with all distributions.

  20.   Victor said

    Sometimes it works on all PCs but you have to add the mbr and syslinux to the usb to the process; but this is long and tedious.
    The easy part about unetbooting

    Be careful: it is not the iso or the dd for me, it is the way you record it.

  21.   danny said

    I have a doubt if I use this command:
    sudo dd if = / dev / zero of = / dev / diskdrive
    Instead of this, the low level formatting is the same or takes longer than this:
    sudo dd if = / dev / zero of = / dev / diskdrive bs = 1M ..

  22.   arg0s said

    Can the dd command be used to delete a specific file?

    eg

    dd if = / dev / urandom of = / home / myfile delete

  23.   arg0s said

    with bs = 1M it becomes faster

  24.   alex said

    And if I am going to make an exact copy from one disk to another, with all its partitions (swap, sda ...) and both disks the same, but one is on the PC and the other on a NAS over the network ????
    Thank you

  25.   Gerardo said

    A comment, I use it with status = progress at the end and I don't need pv, so debian works very well for me to see the progress.
    And the second, is there a way to get only the data and not the whole unit, since if it is a 2TB disk and I only use 100 GB, I require all that space. Is there a way to just back up that 100GB? And when I pass it to another medium it could be a smaller disk maybe one of 500

  26.   Alejandro said

    Help !!!
    Perform the command
    dd if = cbpp-9.0-amd64-20170621.iso of = / dev / sdb

    all good ... but now I want to use the usb as a storage unit and ...

    The USB is not mounted, it does not even appear in GParted.

    Does anyone know what can I do.

    PS: which would be the best program to recover information from discs and CD DVDs….

    1.    Anonymous said

      you must re partition test with fdisk