fdisk command in Linux with examples
Last Updated :
01 Sep, 2024
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.
Similar Reads
eval command in Linux with Examples
eval is a built-in Linux command that executes arguments as a shell command. It combines arguments into a single string, uses it as input to the shell, and executes the commands. Syntaxeval [arg ...]Here, arg can be one or more arguments that eval will process and combine into a single shell command
2 min read
ex command in Linux with examples
ex (stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transf
4 min read
exec command in Linux with examples
The 'exec' command in Linux is a powerful shell built-in used to replace the current shell process with a new command process. Unlike typical commands that create a new process, 'exec' transforms the existing process, directly impacting the efficiency and behavior of scripts. What is the exec Comman
3 min read
exit command in Linux with Examples
The 'exit' command is a fundamental command in Linux used to exit the current shell session. This command is widely used in scripting and command-line operations, allowing users and scripts to terminate a session or a process in a controlled manner. The 'exit' command can take an optional parameter
2 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if
3 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script
2 min read
export command in Linux with Examples
The 'export' command is one of the essential built-in commands in the Bash shell, allowing users to manage environment variables effectively. It is defined in POSIX standards, which state that the shell will assign the export attribute to specified variables, causing them to be included in the envir
3 min read
expr command in Linux with examples
The expr command in Unix is a versatile tool used for evaluating expressions in the shell environment. It performs a wide range of functions, including arithmetic calculations, string operations, and comparisons based on regular expressions. What is the 'expr' Command?expr stands for "expression" an
3 min read
factor command in Linux with examples
The factor command in Linux is used to print the prime factors of the given numbers, either given from the command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntaxfactor [NUMBER]You can provide one or more numbers as argu
2 min read
fc Command in Linux with Examples
As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
3 min read