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/sdb1

Prepare the Drive

Fire up fdisk

fdisk /dev/sdb

If you already have a filesystem on the disk, wipe it.

d

Now create a new primary partition.

p

Enter the partition number.

1

It’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
83

Write the changes to disk (this can’t be undone).

w

Create the Filesystem

Format the new partition using Ext4.

mkfs.ext4 /dev/sdb1

It’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.