How to Manage Linux Disk Partitions using fdisk commands
5 mins read

How to Manage Linux Disk Partitions using fdisk commands

Description

fdisk stands (for “fixed disk or format disk“) is an most commonly used command-line based disk manipulation utility for a Linux/Unix systems.

With the help of fdisk command we can view, create, resize, delete, change, copy and move partitions on a hard drive using its own user friendly text based menu driven interface.

It helps in creating space for new partitions, organising space for new drives, re-organising an old drives and copying or moving data to new disks.

It allows you to create a maximum of four new primary partition and number of logical (extended) partitions, based on size of the hard disk you have in your system.

You must be root user to run fdisk command, otherwise you will get a “command not found” error.

 

1. View all Disk Partitions in Linux

Below command list all existing disk partition on your system.

The ‘-l‘ argument stand for (listing all partitions) is used with fdisk command to view all available partitions on Linux.

The partitions are displayed by their device’s names. For example: /dev/sda, /dev/sdb or /dev/sdc.

[root@mail ~]# fdisk -l

Disk /dev/xvda1: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/xvda2: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

 

2. View Specific Disk Partition in Linux

To view all partitions of specific hard disk use the option ‘-l‘ with device name. For example, the following command will display all disk partitions of device /dev/sda.

For different device names, simple write device name as /dev/sdb or /dev/sdc.

[root@mail ~]# fdisk -l /dev/sda
Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2624 20972857+ 83 Linux
/dev/sda3 2625 4582 15727635 83 Linux
/dev/sda4 4583 77541 586043167+ 5 Extended
/dev/sda5 4583 5887 10482381 83 Linux
/dev/sda6 5888 7192 10482381 83 Linux
/dev/sda7 7193 7845 5245191 83 Linux
/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris
/dev/sda9 8368 77541 555640123+ 8e Linux LVM

 

3. Check all Available fdisk Commands

Use the following command by mentioning the hard disk name such as /dev/sda as shown below.

The following command will give you output similar to below.

[root@mail ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help):

Type ‘m‘ to see the list of all available commands of fdisk which can be operated on /dev/sda hard disk.

After, I enter ‘m‘ on the screen, you will see the all available options for fdisk that you can be used on the /dev/sda device.

 

[root@mail ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help):

 

4. Print all Partition Table in Linux

To print all partition table of hard disk, you must be on command mode of specific hard disk say /dev/sda.

[root@mail ~]# fdisk /dev/sda

 

From the command mode, enter ‘p‘ instead of ‘m‘ as we did earlier.

As I enter ‘p‘, it will print the specific /dev/sda partition table.

Command (m for help): p
Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2624 20972857+ 83 Linux
/dev/sda3 2625 4582 15727635 83 Linux
/dev/sda4 4583 77541 586043167+ 5 Extended
/dev/sda5 4583 5887 10482381 83 Linux
/dev/sda6 5888 7192 10482381 83 Linux
/dev/sda7 7193 7845 5245191 83 Linux
/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris
/dev/sda9 8368 77541 555640123+ 8e Linux LVM
Command (m for help):

 

5. How to Format a Partition in Linux

After the new partition is created, don’t skip to format the newly created partition using ‘mkfs‘ command.

Type the following command in the terminal to format a partition.

Here /dev/sda4 is my newly created partition.

[root@mail ~]# mkfs.ext4 /dev/sda4

 

6.How to Check Size of a Partition in Linux

After formatting new partition, check the size of that partition using flag ‘s‘ (displays size in blocks) with fdisk command.

This way you can check size of any specific device.

[root@mail ~]# fdisk -s /dev/sda2
5194304

 

We hope you’ve found this useful!

Leave a Reply

Your email address will not be published. Required fields are marked *