Linux is a beautiful platform that offers a number of built-in tools. Using those tools, it’s possible to perform a number of complex tasks without any additional toolkits. Having a good command over them can help in a number of situations.
“fdisk” is such a tool that comes pre-installed on almost all the Linux distros. The tool works in the command line interface. Yet, it’s very simple and hassle-free. It’s a partitioning tool for HDDs, SSDs, USB thumb drives etc. Today, we’ll be checking out the usage of “fdisk”.
When you work with “fdisk”, it doesn’t immediately complete the operation. It holds the changes into memory and will perform the action only when you tell it to.
Identifying the “device identifier”
In the case of Linux, all the block devices or HDDs are identified by unique identifier names, for example, “sda”, “sdb” etc. Before proceeding, it’s important to make sure that you’re working on the right device. Otherwise, you may accidentally lose data from another device and find yourself in a mayhem of troubles.
Run the following command –
sudo lsblk
You can easily find out what device you’re about to modify. In my case, my 4GB USB flash drive is tagged as “sdb”.
Opening storage device with fdisk
For performing operations on a device, you have to lock it under fdisk. For that purpose, no partition of the device is allowed to be mounted.
From the above command, you can find out what partition was mounted.
Unmount the partition –
sudo umount /dev/sdb1
Now, it’s time to lock “sdb” under fdisk.
sudo fdisk /dev/sdb
“fdisk” usage
-
Listing available partitions
Once the device is locked under fdisk, enter the following value –
p
You’ll be presented with a list of available partitions.
-
Creating a new partition
A partition table is quite important as it holds the info about the partition(s) of the device. As of the time of publishing this article, there are a few available partition tables –
- DOS – One of the oldest partition tables developed by Microsoft. DOS partition table can’t hold more than 4 primary partitions. Highly suitable for small storage devices.
- GPT – A newly developed partition table that allows more than 4 primary partitions. Extremely useful for big storage devices like HDD, SSD etc.
If you wish to create a DOS partition table, enter the following value –
o
For a GPT partition table, use this value –
g
-
Creating/removing partitions
If you want to delete a partition, enter this value –
d
Then, you’ll be asked for entering the partition number. Make sure to find out the list of partitions using the previous method.
For creating a new partition, enter the following value –
n
At first, you have to select the partition number. Hit “Enter” as the default value is always alright.
Now, you’ll be asked for the “First sector” number. Most of the cases, it’s also nothing to touch about (unless you’re sure what you’re doing). Just hit “Enter”.
Next, time to enter the size of the partition. For the partition size, it must not come up in fractions. For example, you want the partition size of 512MB, type “+512M” (without quotes). Similarly, for 2GB partition, the command will be “+1G”.
As you can see, the partition is created.
Another example –
-
Finalizing all the modifications
After you’ve performed all the modifications, you can now finalize the real changes in your drive. Enter the following value –
w
The writing process is complete now but the partition(s) you created won’t be usable until you format them properly. Run the following command for formatting the partition in “ext3” format –
sudo mkfs.ext3 -L demoPartition /dev/sdb2
Here, “demoPartition” is the name of the partition. You can use any name as you like.
Now, the partition is ready for mounting. Run the following command –
sudo mount /dev/sdb1 /mnt
Verify the result –
df -h
Enjoy!