Formatting drives is easy(if you know what you are doing) in Linux but with a tiny mistake, you can lose data or due to wrong formatting you may also corrupt some important files. So in this article, we will tell you about some basic formatting commands in Linux. Even though you can use number graphical tools available in Linux to manage your drives but there will be some cases where you may get errors and fail to format a drive due to some issues but with a simple command, you can do all that in the terminal which is much easier and fail-safe.
lsblk command: This command tells you about the all the devices connected to your machine.
NAME: It is the column which shows name of the device.
MAJ:MIN: It shows major and minor device number.
sda1 and sda2 denotes the partitions on devices, sdb is USB, sda is SSD, if you have an internal hard drive it will show as sdc.
Steps for Formatting the Drive
Here, we are using a virtual machine for the tutorial but the commands and process will be the same for all kinds of drive and machines. So we will be formatting the 3.7 GB sdb(USB Drive) for this tutorial. You can perform it for any storage device whether internal or external. There are a number of tools in Linux for partitioning drives but we will use ‘parted‘ because it is easy to use. We will use MBR partitioning (‘parted’ also supports GPT partitioning’).
Step 1: Below command will open the parted utility and then you will work inside that.
sudo parted /dev/sdb
Step 2: It creates the partition table and MBR will be used by msdos. You may see warnings regarding partition, just make sure you are partitioning the right drive and enter ignore/y to bypass the warnings.
Step 3: The basic syntax for partition command
mkpart 'partition type' 'file system' start end
If you want to use all space and create one partition use:
mkpart primary ext4 1MiB 100%
if you want to create more than one partition use:
mkpart primary ext4 1MiB 2GB
To create another partition, use:
mkpart primary ext4 2GB 3GB
Here 2 GB is the starting point for this partition because it was an endpoint in the last one and 3GB is the endpoint of this partition. The endpoint can be changed according to what size partition you want, here it will create the second partition of size 1 GB.
Step 4: To see the result of partition use print command, it will display all partitions you have created.
Step 5: If you are satisfied with your partitions, use exit command
Now running ‘isblk‘ will show all the new partitions. You can use any desired file type such as NTFS, vfat, btrfs, etc.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.