Master the Linux ‘fdisk’ Command: A Comprehensive Guide

Peter Hou
3 min readJun 7, 2023

--

This article provides a comprehensive overview of the 'fdisk' command in Linux, including its history, usage, and parameters. It also presents some common use cases, as well as a few uncommon tricks and important points to note.

Instructions

This guide provides a comprehensive overview of the Linux 'fdisk' command for beginners. We'll delve into its history, usage, common parameters, and real-world use cases. Additionally, we will explore some uncommon usage tricks and important points to note.

History

The 'fdisk' command has been a part of Unix and Unix-like operating systems since the 1980s. It is a command-line utility used for disk partitioning.

When and why to use it

'fdisk' is used when you need to create or manipulate disk partitions. It is essential when setting up new drives, resizing existing partitions, or diagnosing disk issues.

How to use it

To use 'fdisk', type 'fdisk' followed by the device identifier. For example, to display the partition table of the first disk:

$ fdisk -l /dev/sda
Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0x7c3a3a20

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 16777182 16775135 8G 83 Linux

The commonly used parameters

  • -l This option lists the partition tables for the specified devices.
$ fdisk -l /dev/sda
Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors
  • -s This option provides the size of a partition.
$ fdisk -s /dev/sda1
8000000

Other supported parameters

  • -b Specify the sector size of the disk.
  • -u Change display/entry units.
  • -C Set the number of cylinders.
  • -H Set the number of heads.
  • -S Set the number of sectors per track.

Most common use cases

One of the most common use cases for 'fdisk' is creating a new partition on a disk.

$ sudo fdisk /dev/sda

Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (20971648-167772159, default 20971648):
Last sector, +sectors or +size{K,M,G,T,P} (20971648-167772159, default 167772159):

Created a new partition 3 of type 'Linux' and of size 69 GiB.

The tricky skills

While 'fdisk' is mainly used for creating and managing disk partitions, it can also be used to display the disk space used and available on all partitions.

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.8G 1.1G 6.3G 15% /

What needs to be noted

  • 'fdisk' requires root privileges. Always use caution when operating as the root user.
  • Changes made with 'fdisk' are immediate and can result in data loss if used improperly.

Conclusion

'fdisk' is an extremely useful tool for managing disk partitions in Linux. This guide provides a comprehensive overview of its usage and parameters, equipping beginners with the knowledge they need to utilize 'fdisk' effectively.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet