Creating, deleting and formatting Partitions in Linux from the Command Line

- by

Here’s another personal favourite topic my brain likes to forget: how to create and format disk partitions from the Command Line in Linux. No matter how many times I do it, I always forget the details when I don’t have a GUI to hand. For the record, both KDE and GNOME as well as the Cockpit Web Console do a much better job at this than hacking text commands, but sometimes we just don’t have a choice.

So here goes, enjoy! I’m using Fedora for this demo, but I’m pretty sure this will work with any distro.

Checking Drives on your system

The first thing we need to find is what your drive is called. Windows likes referring to things like “Disk 0” and “Disk 1”, but in Linux everything’s a device so we’re looking for something along the lines of /dev/sdx (where x is the drive in question). LSBLK will show us this.

lsblk

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 388.4M  1 disk
sdb      8:16   0    32G  0 disk [SWAP]
sdc      8:32   0     1T  0 disk /mnt/wslg/distro
                                 /
sdd      8:48   0   7.3T  0 disk
└─sdd1   8:49   0   7.3T  0 part
sde      8:64   0   7.3T  0 disk
└─sde1   8:65   0   7.3T  0 part
sdf      8:80   0   7.3T  0 disk
└─sdf1   8:81   0   7.3T  0 part
sdg      8:96   0   7.3T  0 disk
└─sdg1   8:97   0   7.3T  0 part
sdh      8:112  0 931.5G  0 disk
└─sdh1   8:113  0 931.5G  0 part

We’ll focus on /dev/sdh here, the drive at the bottom (confusingly, my system is also showing several others but we’ll ignore those).

Removing existing Partitions

My drive already has a partition (/dev/sdh1), and it’s healthy and good, but let’s say it’s something I’d rather wipe before I get started. Perhaps I want to partition my drive into two parts later, and this was something I’ve done in Windows, so I want to get rid of it. FDISK can do this for us.

Now FDISK is a little programme of its own that we can enter and navigate, much like DISKPART on Windows. We type the device in question, then issue one-letter commands that only affect the selected device. That way we can’t accidentally wipe out something on another drive, which I find really handy.

fdisk /dev/sdh

Welcome to fdisk (util-linux 2.40.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

Type m and hit return to see a list of exciting things we can do with FDISK. I’m going to use P and print all partitions on this device, revealing what’s currently there.

Command (m for help): p

Disk /dev/sdh: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: Samsung SSD 870
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: EBFF3CB3-2537-4012-940F-0EA30C10230C

Device     Start        End    Sectors   Size Type
/dev/sdh1   2048 1953523711 1953521664 931.5G Linux filesystem

Let’s remove this partition now with D. Since there’s only one on here, it’ll become the default and all we need to do is follow the prompts.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Wow, that was fast! Thankfully we haven’t written those changes to the disk yet, so if you’ve made a mistake don’t panic. Hit q and you’ll leave FDISK without this change taking effect. However, if you press w you will write those changes to disk and quit FDISK.

Creating a new Partition

We don’t want to leave just yet, because we can to create our own partition. In fact, let’s two just for kicks. The N command can do that for us.

Command (m for help): n
Partition number (1-128, default 1):
First sector (34-1953525134, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1953525134, default 1953523711): +100G

Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: y

The signature will be removed by a write command.

Quite a lot going on here, but it’s fairly simple to follow. N creates a new partition, by default starting at 1 if we don’t have existing partitions. Next it wants to know where to start and end. You can hit enter without any values, which will create Partition 1 and use the full space available. I didn’t want that, so I pressed enter for the start sector, then typed “+100G” to end 100GB after the start, which will create a 100GB partition for me.

I also got a little warning that I had an existing signature from the previous partition. FDISK offered to wipe it out and I agreed (we’ll make our own later on). Now we can check if this has worked by typing P one more time:

Device     Start       End   Sectors  Size Type
/dev/sdh1   2048 209717247 209715200  100G Linux filesystem

It’s all good, we have one partition of 100GB. Let’s make another one with the rest of the available space, just like before, accepting all default values.

Device         Start        End    Sectors   Size Type
/dev/sdh1       2048  209717247  209715200   100G Linux filesystem
/dev/sdh2  209717248 1953523711 1743806464 831.5G Linux filesystem

Very nice! All that remains to be done is to write these changes to disk with the W command before we can put these partitions to good use.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Formatting Partitions

Now that we have space, let’s put a file system on it so we can use it. This process is known as formatting of course, and depending on how you want to use this space, there’s plenty of options to choose from in Linux. The most popular file system is EXT4, but Fedora can also handle NTFS, BTRFS, XFS and many others. Since we have two partitions, let’s format the first one with EXT4 and the second one with NTFS just to see the process.

The tool we use for that is called MKFS, and it needs to know which partition and what file system to use. It even has a convenient shorthand method. Here’s how we format our first partition:

mkfs.ext4 /dev/sdh1

mke2fs 1.47.1 (20-May-2024)
Discarding device blocks: done
Creating filesystem with 26214400 4k blocks and 6553600 inode
Filesystem UUID: b9d1d9db-79dd-48da-947a-a6f97dfb2d06
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done

We can use similar shorthand to make an NTFS file system:

mkfs.ntfs -f /dev/sdh2

Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

The -f parameter tells MKFS to perform a quick format instead of filling all sectors with zeros (doing to takes a bit longer and isn’t necessary, especially if you’re using an SSD).

Mounting our Partitions

There’s only one more thing left to do: mounting out partitions so that our system can interact with it (i.e. read from and send data to). If you’re using a GUI this often happens automatically, but on the command line we need to do this step manually. It’s pretty straightforward, but we need a valid location (or two) first.

Let’s create /mnt/part1 and /mnt/part2 for these drives (/mnt already exists and is commonly used to add mounted devices to). We need to be sudo for this to work:

mkdir /mnt/part1 /mnt/part2

This will create two empty directories. Now we’ll mount our partitions like so:

mount /dev/sdh1 /mnt/part1
mount /dev/sdh2 /mnt/part2

That’s it! Note that this mount won’t survive a restart, which may or may not be what you want. To make it permanent, it requires an entry in the /etc/fstab file (but that’s a story for another time).

Happy Hacking!



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.