common fdisk commands in linux

Most Common Fdisk Commands in Linux

fdisk (for fixed disk or format disk) is one of the most commonly used commands in Linux. It allows you to view, create, resize, change, copy, delete, move partitions using its user friendly interface. In this article, we will learn some of the most common fdisk commands in Linux. It is very useful for creating space for new partitions, re-organizing existing partitions, copying or moving data to new disks.


Most Common Fdisk Commands in Linux

We will learn some of the most common fdisk commands in Linux.

1. View All Disk Partitions in Linux

The simplest command to list all Linux partitions in Linux is the following, where -l stands for listing.

# fdisk -l

2. View Specific Partitions in Linux

If you want to view specific partitions of a device in Linux, use -l option followed by the device/disk name. For example, here is the command to display disk partitions of /dev/sda.

# fdisk -l /dev/sda

3. Check Available fdisk commands

If you want to get a list of all available fdisk commands, you can do it with the following command.

# fdisk /dev/sda

When you see a prompt, enter ‘m’ to see all available options.

4. Print All Partition Table

To print all partition table, you need to run the same command used above.

# fdisk /dev/sda

But in this case, when you see a prompt, enter ‘p’ instead of ‘m’.

5. Delete Partition

If you want to delete a partition, enter fdisk command followed by the device which contains this partition.

# fdisk /dev/sda

You will see a prompt. Enter ‘d’ to delete partition. You will be prompted to enter partition number. If you enter the number 4, it will delete partition /dev/sda4.

Command (m for help): d
Partition number (1-4): 4

Next, enter ‘w’ to save changes and exit the command.

Command (m for help): w
The partition table has been altered!

But please note, the new changes will be applied only after the next system reboot. Also, when you delete a partition this way, it deletes all the files & data in it.

6. Create New Partition

If you want to create new partition in device /dev/sda go to its command mode using the following command.

# fdisk /dev/sda

When you see a prompt, enter ‘n’ to create new partition. Next, you will see 2 options – p for primary, e for extended. Once you enter either of these options, you will be asked to enter the following information.

First cylinder number of the partition to be create.
Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).

You can enter the size of cylinder as +500M where + means addition and 500M means 500Mb. Finally, you should enter ‘w’ option to save changes. Lastly, reboot your system to apply changes.

7. Format Disk Partition

Once you have created the new partition, it is essential to format it using mkfs command. Here is the command to format the newly created partition /dev/sda4 as ext4.

# mkfs.ext4 /dev/sda4

8. Check Size of Partition

If you want to check the size of Linux partition, you need to use -s option with fdisk.

# fdisk -s /dev/sda2
5194304

In this article, we have learnt several useful ways to create, view, organize and delete disk partitions in Linux.

Also read:

How to Convert Home Directory into Partition in Linux
How to Check Remote Port Open in Linux
How to Generate GRUB Password in Linux
How to Boot Into Single User Mode in CentOS
How to Restore Delete Tmp Directory in Linux

Leave a Reply

Your email address will not be published.