From LQWiki
Jump to: navigation, search

fdisk is a tool for allowing you to modify the partitions in an MSDOS partition table on a hard drive. See gdisk for GPT partitions.

The tool is not recommended for inexperienced or beginning users, since it can potentially destroy all your data on the hard drive and make a system un-bootable from it. MSDOS partitions are also becoming obsolete, and GPT partitions are replacing them because of size limitations.

  • Note: there is a tool named fdisk under Windows and DOS - it does the same thing as the fdisk in Unix and Unix like operating systems.


List the partition table of the first hard disk (using the Linux naming scheme for disks)

fdisk -l /dev/hda

Lists the partitions on all available hard disks (handy for troubleshooting and remembering which partition is where):

fdisk -l

Using fdisk

To know what hard drives you can use to create partitions on, use the command:

hwinfo --block --short

In this example we will delete all partitions on a disk and create a new partition. Start fdisk with the hard drive you wish to partition as argument:

$ fdisk /dev/sda

The number of cylinders for this disk is set to 14593.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):       

Type m for help, you will find that you need to type d to delete a partition:

Command (m for help): d
Selected partition 1

fdisk has automatically selected partition 1 and deleted it, because it was the only one. Use the command p to show all partitions and prove all are deleted:

Command (m for help): p

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Create a new partition with the command n:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-14593, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-14593, default 14593):
Using default value 14593

Write the partition table with the command w

Command (m for help): w

Quit the program with the command q

Command (m for help): q

Partition layout for WinDos

If you create a FAT partition that you intend to format in DOS or Windows, zero the first block of the partition so that the Microsoft product doesn't misinterpret the parameters. If you just created /dev/sdb9, you can do the following

dd if=/dev/zero bs=1k count=1 of=/dev/sdb9

Provided by

Most (all?) Linux distributions incorporate this from the [util-linux] project.

See also