Ubuntu – wait for dd command to fully write to the disk

bashcommand line

I'm using the dd command to create a bootable usb from iso file:

sudo dd if=~/Desktop/ubuntu.iso of=/dev/sdx bs=1M

After pressing enter it momentarily exits and gives me:

915+0 records in 915+0 records out 959447040 bytes (959 MB) copied,
0.539375 s, 1.8 GB/s

So it's like running in background because I can see that the flash drive is working. Eventually it will stop copying and I can remove the drive successfully but the question is why doesn't dd command wait for copying to finish. Why does it run in background. And how can I make it wait?

Best Answer

Despite popular belief, dd is a perfectly ordinary command, it isn't more low-level than cat or cp. Your command reads from the disk cache and writes to the disk buffers like any other command.

In order to make sure that the data is fully written to the physical media, you need to call sync. The command sync flushes all output buffers to the disk(s). When the sync command returns, the data has been fully written.

sudo dd if=~/Desktop/ubuntu.iso of=/dev/sdx bs=1M; sync

Most of the time, you don't need to call sync, because unmounting a filesystem does the same job. When the umount command returns, or when you get a confirmation message after clicking “Eject”, the buffers have been written to the disk. Here, you're directly writing to the disk without going through a mounted filesystem, so you need to explicitly flush the buffer.

Note that instead of dd, you could use tee. This has two advantages: there's less risk of inverting the source and destination due to a typo, and it's probably slightly faster.

<~/Desktop/ubuntu.iso sudo tee /dev/sdx >/dev/null; sync
Related Question