It's for a bash script. Basically, I want to format, or erase a USB (or SD) storage device; with a single command line.

I was going to use fdisk, but it seems to require user interaction where I want automation.

So then I decided to try zeroing it out with:
dd if=/dev/zero of=/dev/<target disk>;
but it only seems to zero 2.0 GB of vacant, or unused disk space.


root@linux:~# dd if=/dev/zero of=/dev/mmcblk0
dd: writing to '/dev/mmcblk0': No space left on device
3842249+0 records in
3842249+0 records out
1967230976 bytes (2.0 GB, 1.8 GiB) copied, 2.9054 s, 677 MB/s

Ideally, I'm talking about re-formatting a removable storage device, and prepping it to be imaged with an .iso image file (via dd).

Re-formatting won't always be required, but it also erases data; and clearing the device of any stored data probably ought to be the default behaviour / standard procedure, for this kind of thing anyway.

  • What exactly do you mean by "format"? If you simply want all data deleted / zeroed, your dd command line should suffice. – crater2150 Feb 13 '17 at 10:18
  • 1
    what is your real goal? zeroing? formatting (to what filesystem?) - as per your title? both? – Jeff Schaller Feb 13 '17 at 12:07
  • @crater2150 Actually, the dd command didn't seem to work for me. I've added details to the question body. – tjt263 Feb 15 '17 at 1:17
  • @JeffSchaller Ideally, I'm talking about re-formatting a removable storage device, and prepping it for .iso imaging via dd. Re-formatting won't always be required, but clearing of any stored data should probably be the default behaviour anyway. – tjt263 Feb 15 '17 at 1:18
  • How large is the device, in GiB? – Mioriin Feb 15 '17 at 2:11
up vote 2 down vote accepted

If you want to use fdisk, with only one partition, with all blocks used, this will suffice:

echo -e "n\np\n1\n\n\nw\n"| 
fdisk /dev/<target disk> && 
mkfs.ext4 /dev/<target disk>

Change mkfs.ext4 to whatever filesystem type you want it to use.

If you just want to delete data, your dd command should be fine.

  • Thanks; I'll try it. How does the first bit work, exactly? – tjt263 Feb 14 '17 at 20:44
  • Also, the dd thing didn't actually work for me. It seems to overwrite 1967230976 bytes (2.0 GB, 1.8 GiB) only; and also indicates: No space left on device. I've added more details to the question body. – tjt263 Feb 15 '17 at 1:20
  • 1
    the echo at the beginning simulates entering "n [return] p [return] 1 [return][return][return] w [return]" - these are the keys you would press manually within fdisk to select 1) new partition 2) primary partition 3) partition 1 4) start on lowest block size 5) end on highest block size (i.e. to use all available space on the disk) 6) write partition table & exit – RobotJohnny Feb 15 '17 at 10:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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