Guide - Erasing and Formatting a Disk in Ubuntu
Everything here is being done in Ubuntu 20.04.1 LTS. Make sure to backup any important files before proceeding. My use case was fixing a corrupted drive after a failed OS install due to a power outage.
- first you want to find your disk file
- /dev/ is the location of your disk files. Remember that in a linux filesystem everything is a file or a directory
- type "lsblk" in terminal to use the list block devices tool -- it will show disk size in mb, gb, or tb so that you can easily find what you are looking for
- find your target disk -- in my case it was /dev/sdb
- lastly, type:
- it is very important that you unmount the disk before wiping it with the dd tool
sudo umount /dev/sdb/
- /dev/zero is an endless stream of null characters
- basically it converts all 1s to 0s on your target drive effectively wiping the drive completely
- status flag produces a progress bar you can watch
- "bs" is bytes(convert x bytes at a time) and 4096 is a good default -- for optimal speed you have to look up write speeds for specific hardrives
- "if" is source and "of" is destination
- type this to begin the wiping process -- remember that it may take hours depending on how big the disk is. It took about 6000 seconds to write from an SSD to the entirety of a 1TB HDD:
sudo dd if=/dev/zero of=/dev/(disk) status=progress bs=4096
- use fdisk to add a primary partition
- Disks require a partition or container with a file system in order to be read/written. A single primary partition will do, but you can have any number of partitions according to how you want to organize files.
- type this -- in my case the disk is /dev/sdb:
- type "n" and press enter to add a new partition
- type "m" and press enter to add a primary partition
- type "1" and press enter to add a single partition
- type "w" and press enter to write the parition to the disk
- you should now have a partitioned disk thats read to be formatted with parted
sudo fdisk /dev/(disk)
- type these in order -- in my case it is /dev/sdb
- here I use GPT to make a partition tabel, set the byte unit to terabyte, TB, call the partition "primary", set the system type to ext4(ext4 is optimal for linux), start the tabel at 0(beginning of disk), and finally end the tabel at 1 -- I end at 1 because my disk was 1 terabyte in size:
- once it finises type "print" to see your primary partition
- type "quit" to exit parted
sudo parted /dev/(disk)mklabel gptunit TBmkpartprimaryext401
- type "lsblk" again to see your newly cleaned disk and its single, primary partition
- make a new directory for mounting devices -- in my case it is /mnt/sdb1:
- finally mount the disk:
- everything should work flawlessly
sudo mkdir /mnt/(disk)
sudo mount /dev/(disk) /mnt/(disk)