Quickest way to format a disk with Linux?

10,997

So, this depends on

  1. your definition of 'formatting' a drive;
  2. the type of that drive.

"Format" might be one of the most confusing terms in consumer tech, really. Even your post already confuses two or three different actions...

Normally it covers three levels:

  1. Preparing the media itself (e.g. writing sector markers to a HDD) – nowadays usually done by the factory, but at least in IBM PC days it could be done by the PC itself. This is often called "low-level formatting".

  2. Writing a partition table (either blank or with some partitions) to the first few sectors of the disk – this is what fdisk does.

  3. Writing a blank filesystem into each partition – this is what format c: or mkfs do. This used to be called "high-level formatting".

Except some people call step #2 "low-level format" now that there's no #1 anymore. And sometimes people call erasing the whole disk "formatting" even though it doesn't write anything with an actual format in there. Sometimes people call reinstalling Windows "formatting" even if all the files are left untouched.


Anyway, the rest of the answer depends on the result you want:

  • If you want the disk to be completely blank (e.g. nothing that a data recovery app could possibly find), you have two options: discarding or manual erasing.

    The "discard" feature is called TRIM in SSDs and also supported by SD cards – it tells the disk itself to throw away all data. On Linux, the blkdiscard tool can be used for this purpose – specify a device and within seconds it's empty. (Tools like mkfs.ext4 will also automatically discard the partition's contents before writing the new filesystem into it.)

    If the disk doesn't support discard – e.g. if it's a magnetic HDD, or if it's a USB flash drive whose Mass Storage interface doesn't understand this command – then your only other option is manually blanking out each and every sector, i.e. filling it with /dev/zero as you've already done.

    While discard usually takes only a few seconds, manual zero-filling cannot go any faster than the disk itself can accept writes (after all, you are writing a lot of data).

  • If you don't care about residual data, but merely want the OS to see an empty disk, you can just blank out the area where partition table resides. For MBR that's always sector 0, but don't forget that many disks use a GPT partition table.

    This is a very fast method since you only have to erase a few kilobytes at most. On Linux, the wipefs command will surgically make disk contents no longer recognizable to the OS. (By default the tool only prints out what it would erase – use the option --all to make it actually do it.)

Eitehr way, now you have a disk that the OS considers to be empty.

  • If you want the erased disk to have some partitions, use partitioning tools like fdisk or parted. While regular fdisk is interactive, there are plenty of tools which can be easily scripted – sfdisk, sgdisk, parted are a few examples. Windows has diskpart.

  • If you also want the partitions to have filesystems, use tools like mkfs.ext4 or mkfs.vfat (equivalent to Windows' format).


Finally, as mentioned in the comments above: If you're planning to just write an image (an .iso file) to the disk, then you don't need to do any preparations at all.

Such image writing starts at sector 0, so it trashes everything that was on the disk previously (up to the size of the image). Whether it had partitions and files, or whether it was blank, doesn't matter – it simply gets overwritten with the image's own partitions and all.

Everything beyond the size of the image will be left as it was, but the OS won't care about it – since the image's partition table says there's no partition there. Though of course discarding or blanking the disk is still a good idea if you're going to give it to somebody else.

Share:
10,997

Related videos on Youtube

voices
Author by

voices

Updated on September 18, 2022

Comments

  • voices
    voices over 1 year

    It's for a bash/shell 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 any removable storage device, and/or 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.

  • voices
    voices about 7 years
    Yes, I think I'd rather shred all the old data, just in case. I'll include a function with a read & if, then, else; or case statement. That way the user can decide.
  • Community
    Community about 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • Ravindra Bawane
    Ravindra Bawane about 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review