Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
3  
Have you tried sync? –  choroba Jul 9 '13 at 15:32
1  
@choroba Does it make sense to run 'sync' if the system's already writing data to the flash drive? –  Kirill Ivlev Jul 9 '13 at 16:03
    
I am not sure, byt sync might wait. –  choroba Jul 9 '13 at 16:05
    
dd also has some sync options, for example conv=fsync. That said, I never had to use it with /dev/sd* drives myself. If you literally used /dev/sdx my guess would've been you had a useless 959MB file in /dev (ramdisk) now... –  frostschutz Jul 9 '13 at 16:07
    
@frostschutz I used /sdc for my flash drive –  Kirill Ivlev Jul 9 '13 at 16:23

1 Answer 1

up vote 9 down vote accepted

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
share|improve this answer
1  
How about the "Eject" or "Safely Remove" buttons/icons. Wouldn't they also call sync before giving the "OK to remove" notification? –  user68186 Jul 9 '13 at 16:11
    
You likely want to use sudo tee /dev/sdx >/dev/null, otherwise the copying process would still be very slow due to data being written to console output. –  Lekensteyn Jul 9 '13 at 16:26
1  
@user68186 They don't call the sync command, but they do the same job under the hood. –  Gilles Jul 9 '13 at 16:30
    
@Gilles doesn't the kernel starts writing data to the disk once dd command puts data into memory? –  Kirill Ivlev Jul 9 '13 at 16:35
    
@KirillIvlev It may start (depending on how much RAM is available for the disk buffers). The problem is that it isn't guaranteed to finish. sync tells the kernel “finish now”. –  Gilles Jul 9 '13 at 16:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.