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
fc-cache command in Linux with Examples
The fc-cache command in Linux scans the font directories and build font cache for applications which use fontconfig for their font handling. When no arguments are passed fc-cache scans each directory for font files readable by FreeType. A cache is created which contains properties of each font and t
2 min read
fc-list command in Linux with examples
fc-list command is a part of the fontconfig system. It is used to list the available fonts and font styles. Using the format option, the list of all fonts can be filtered and sorted out. Moreover, multiple parameters can be passed to it after a colon symbol (:) to restrict the information that is be
3 min read
fdisk command in Linux with examples
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 creati
5 min read
fg command in Linux with examples
The fg command in Linux is used to bring a background job into the foreground. It allows you to resume a suspended job or a background process directly in the terminal window, so you can interact with it. Syntaxfg [job_spec]The job_spec is a way to refer to the background jobs that are currently run
3 min read
fgrep command in Linux with examples
The 'fgrep' filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings that contain lots of regular expression metacharacters, such as "^", "$", etc. This makes 'fgrep' particularly v
4 min read
file command in Linux with examples
The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable f
3 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This articl
9 min read
Finger command in Linux with Examples
The 'finger' command is a powerful utility in Linux used to display information about users logged into the system. This command is commonly used by system administrators to retrieve detailed user information, including login name, full name, idle time, login time, and sometimes the user's email add
4 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specif
4 min read
fold command in Linux with examples
The fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. By default, it wraps lines at a maximum width of 80 columns, which is configurable. To fold input using the fold command pass a file or standard input to the command. Syntax of `
3 min read
for command in Linux with Examples
IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of
2 min read
free Command in Linux with examples
While using LINUX there might come a situation when you are willing to install a new application (big in size) and you wish to know the amount of free memory available on your system. In LINUX, there exists a command line utility for this and that is the 'free' command which displays the total amoun
6 min read
Fun Commands in Linux
Linux isn't just for coding and administration—it can also be a lot of fun. With a variety of terminal commands, you can add some entertainment to your Linux experience. Below is a list of some cool and fun commands you can use in Linux to enhance your terminal experience. We’ll also cover how to in
4 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the u
2 min read
Compiling with g++
g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. The different "options" of g++ command allow us to stop this process at the intermediate stage. Check g++ compiler version informa
3 min read