The Wayback Machine - https://web.archive.org/web/20250108162339/https://www.geeksforgeeks.org/fdisk-command-in-linux-with-examples/
Open In App

fdisk command in Linux with examples

Last Updated : 01 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The ‘fdisk’ command in Linux, short for “format disk,” is a powerful, dialog-driven utility used for creating and manipulating the disk partition table. Essential for system administrators and users looking to manage their hard drive’s structure, ‘fdisk’ supports a range of operations such as creating, deleting, and resizing partitions.

Here, we will learn how to use fdisk effectively across various scenarios.

What is ‘fdisk’ command?

fdisk allows you to create a maximum of four primary partitions and the number of logical partitions depends on the size of the hard disk you are using. It allows the user: 

  • To Create space for new partitions.
  • Organizing space for new drives.
  • Re-organizing old drives.
  • Copying or Moving data to new disks(partitions).

Syntax:  

fdisk [options] device

or

fdisk -l [device...]

Here’s a breakdown of some commonly used options in ‘fdisk’:

  • ‘-l’: Lists the partition tables for specified disks or all disks if no disk is specified.
  • ‘-s‘: Reports the size of a specified partition.
  • ‘-d’: Dumps the partition table to a file.
  • ‘-u‘: Shows the exact data placement on the disk in units.
  • ‘-b’: Allows specification of sector size, useful for disks with non-standard sector sizes.

‘fdisk’ command examples in Linux

Let us look at some of the ways in which ‘fdisk’ command can be used in Linux.

1. View All Disk Partitions

The first thing to do before doing any thing with the disks and partition is to view basic details about all available partition in the system. The below command is used to list the partitions on your system and see their /dev names. For example ‘/dev/sda’, ‘/dev/sdb’ or ‘/dev/sdc’. 

$ sudo fdisk -l

2. View Partition on a Specific Disk

Below command is used to view all disk partitions on device ‘/dev/sda’.  

$ sudo fdisk -l /dev/sda

3. View all fdisk Commands

To see all the command which are available under fdisk command you can use ‘/dev/sda’ partition with ‘fdisk’ command.  

$ sudo fdisk /dev/sda

Note: This will prompt for a command. Type ‘m’ for seeing all the operations which can perform on ‘/dev/sda’. After pressing m you will get:  

4. Create a Hard Disk Partition

For this go inside the hard drive partition that is the ‘/dev/sda’ partition, and use the following command:  

$ sudo fdisk /dev/sda

Now you have to type n to create new partition and then type ‘p’ for making a primary partition and ‘e’ for making an extended or logical partition depending on the type of partition to make.  

Run ‘w’ command to write the changes and reboot your system. 

5. Delete a Hard Disk Partition

To delete a partition for the hard disk and free up space occupied by that partition for example ‘/dev/sdb’. Go to the command menu using following:  

$ sudo fdisk /dev/sda

and then type ‘d’ to go to the delete partition menu. It will prompt the partition number you want to delete(type the number).  

Run ‘w’ command to write the changes and reboot the system. 

6. How to view the size of your Partition

$ sudo fdisk -s /dev/sda

Note:  

  • To check for the manual page of ‘fdisk’ command, use the following command:
$ man fdisk
  • To see the help message and listing of all options, use the following command option:
$ sudo fdisk -h

fdisk command in Linux with examples – FAQs

What is the fdisk command used for in Linux?

The fdisk command in Linux is a disk partitioning tool widely used for manipulating disk partitions on Linux systems. It allows users to create, resize, delete, and manage disk partitions on their hard drives. fdisk supports MBR (Master Boot Record) and newer GPT (GUID Partition Table) disk layouts, making it versatile for various storage technologies.

How to create a new partition using fdisk?

To create a new partition using fdisk, follow these steps:

Launch fdisk on the target disk: Replace /dev/sdx with the actual device name of your disk (e.g., /dev/sda).

sudo fdisk /dev/sdx

Enter n to create a new partition: Follow the prompts to specify whether it’s a primary (p) or extended (e) partition. fdisk may ask for the partition number, starting and ending sectors, or size.

Choose partition type (if necessary): For MBR, you may need to decide between primary and extended partitions. Most users will want primary unless they need more than four partitions.

Write the changes: After configuring the partition, enter w to write the changes to disk and exit fdisk.

How to list all partitions on a disk using fdisk?

To list all partitions on a disk using fdisk, you can use the -l option, which shows detailed information about all the partitions on all disks or a specific disk if specified. For example:

sudo fdisk -l

Or to list partitions on a specific disk:

sudo fdisk -l /dev/sdx

Can fdisk be used to format a partition?

No, fdisk itself does not format partitions; it only creates, deletes, and modifies the partition table. To format a new partition, you should use a filesystem creation tool such as mkfs after creating the partition with fdisk. For example, to format a partition with the ext4 filesystem, you would use:

sudo mkfs.ext4 /dev/sdx1

Make sure to replace /dev/sdx1 with the actual device and partition number.

What are some common options for the fdisk command?

  • -l: Lists the partition tables for the specified disks or all disks if no disk is specified.
  • -d: Dump the partition table to a file.
  • -u: Display sectors in units, showing exact data placement on the disk.
  • -t: Change partition type ID, which can be useful for systems that use the partition ID for special purposes.
  • -b: Specify the sector size, useful for disks with non-standard sector sizes.


Next Article

Similar Reads

three90RightbarBannerImg