Format a Linux Disk as Ext4 from the Command Line
There are plenty of guides for how to do this online. But I end up spending 20 minutes searching every time I need to remind myself how to do it, so I’m putting it here to save me that hassle.
For the code, I’m just going to assume the drive is /dev/sdb. If you’re following this, make sure you’re using the correct drive name.
Unmount The Drive
Only needed if it’s been mounted already.
sync
umount /dev/sdb1Prepare the Drive
Fire up fdisk
fdisk /dev/sdbIf you already have a filesystem on the disk, wipe it.
dNow create a new primary partition.
pEnter the partition number.
1It’ll now ask you to set the start and end cylinders on the disk. Just hit return for each to accept the defaults.
Change the system type to Linux.
t
83Write the changes to disk (this can’t be undone).
wCreate the Filesystem
Format the new partition using Ext4.
mkfs.ext4 /dev/sdb1It’ll now go off and start writing the filesystem. This can take a bit of time, but it should give you an update on what’s being written.
Mount Your Drive
Now your partition has been created, you need to mount it somewhere to use it. Here I’m using /mnt/user/ext/.
mnt /dev/sdb1 /mnt/user/ext/And there you go.