0% found this document useful (0 votes)
48 views15 pages

7 Linux Fdisk Command Examples To Manage Hard Disk Partition

Uploaded by

zin lynn naing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views15 pages

7 Linux Fdisk Command Examples To Manage Hard Disk Partition

Uploaded by

zin lynn naing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.

com/2010/09/linux-fdisk/

Eliminate I/O Bottleneck


www.SuperSpeed.com
Increase SQL Performance by 500% Download RamDisk Plus Free

Home
About
Free eBook
Archives
Best of the Blog
Contact

by Balakrishnan Mariyappan on September 14, 2010

7 Like 13 Tweet 18

1 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

On Linux distributions, fdisk is the best tool to manage disk


partitions. fdisk is a text based utility.

Using fdisk you can create a new partition, delete an existing partition, or change existing partition.

Using fidsk you are allowed to create a maximum of four primary partition, and any number of logical
partitions, based on the size of the disk.

Keep in mind that any single partition requires a minimum size of 40MB.

In this article, let us review how to use fdisk command using practical examples.

Warning: Don’t delete, modify, or add partition, if you don’t know what you are doing. You will lose
your data!

1. View All Existing Disk Partitions Using fdisk -l

Before you create a new partition, or modify an existing partition, you might want to view all
available partition in the system.

2 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Use fdisk -l to view all available partitions as shown below.

# fdisk -l

Disk /dev/sda: 80.0 GB, 80026361856 bytes


255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System


/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 2662 2904 1951866 83 Linux
/dev/sda7 2905 3147 1951866 83 Linux
/dev/sda8 3148 3264 939771 82 Linux swap / Solaris
/dev/sda9 3265 5283 16217586 b W95 FAT32

The above will list partitions from all the connected hard disks. When you have more than one disk
on the system, the partitions list are ordered by the device’s /dev name. For example, /dev/sda,
/dev/sdb, /dev/sdc and so on.

2. View Partitions of a Specific Hard Disk using fdisk -l /dev/sd{a}

To view all partitions of the /dev/sda hard disk, do the following.

# fdisk -l /dev/sda

View all fdisk Commands Using fdisk Command m

Use fdisk command m, to view all available fdisk commands as shown below.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.


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)

3 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

2) booting and partitioning software from other OSs


(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): m


Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

3. Delete a Hard Disk Partition Using fdisk Command d

Let us assume that you like to combine several partitions (for example, /dev/sda6, /dev/sda7 and
/dev/sda8) into a single disk partition. To do this, you should first delete all those individual partitions,
as shown below.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.


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): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes


255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System


/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 2662 2904 1951866 83 Linux
/dev/sda7 2905 3147 1951866 83 Linux
/dev/sda8 3148 3264 939771 82 Linux swap / Solaris
/dev/sda9 3265 5283 16217586 b W95 FAT32

Command (m for help): d


Partition number (1-9): 8

Command (m for help): d


Partition number (1-8): 7

Command (m for help): d

4 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Partition number (1-7): 6

Command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

4. Create a New Disk Partition with Specific Size Using fdisk Command n

Once you’ve deleted all the existing partitions, you can create a new partition using all available
space as shown below.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.


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): n


First cylinder (2662-5283, default 2662):
Using default value 2662
Last cylinder, +cylinders or +size{K,M,G} (2662-3264, default 3264):
Using default value 3264

In the above example, fdisk n command is used to create new partition with the specific size. While
creating a new partition, it expects following two inputs.

Starting cylinder number of the partition to be create (First cylinder).


Size of the partition (or) the last cylinder number (Last cylinder, +cylinders or +size ).

Please keep in mind that you should issue the fdisk write command (w) after any modifications.

Command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

After the partition is created, format it using the mkfs command as shown below.

# mkfs.ext3 /dev/sda7

5. View the Size of an existing Partition Using fdisk -s

5 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

As shown below, fdisk -s displays the size of the partition in blocks.

# fdisk -s /dev/sda7
4843566

The above output corresponds to about 4900MB.

6. Toggle the Boot Flag of a Partition Using fdisk Command a

Fdisk command displays the boot flag of each partition. When you want to disable or enable the boot
flag on the corresponding partition, do the following.

If you don’t know why are you are doing this, you’ll mess-up your system.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.


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): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes


255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System


/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 3265 5283 16217586 b W95 FAT32
/dev/sda7 2662 3264 4843566 83 Linux

Partition table entries are not in disk order

Command (m for help): a


Partition number (1-7): 5

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes


255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System


/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 1960 2661 5638752 83 Linux
/dev/sda6 3265 5283 16217586 b W95 FAT32
/dev/sda7 2662 3264 4843566 83 Linux

Partition table entries are not in disk order

6 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Command (m for help):

As seen above, the boot flag is disabled on the partition /dev/sda5.

7. Fix Partition Table Order Using fdisk Expert Command f

When you delete a logical partition, and recreate it again, you might see the “partition out of order”
issue. i.e “Partition table entries are not in disk order” error message.

For example, when you delete three logical partitions (sda6, sda7 and sda8), and create a new
partition, you might expect the new partition name to be sda6. But, the system might’ve created the
new partition as sda7. This is because, after the partitions are deleted, sda9 partition has been
moved as sda6 and the free space is moved to the end.

To fix this partition order issue, and assign sda6 to the newly created partition, execute the expert
command f as shown below.

$ fdisk /dev/sda

The number of cylinders for this disk is set to 9729.


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): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes


255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System


/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 3265 5283 16217586 b W95 FAT32
/dev/sda7 2662 3264 4843566 83 Linux

Partition table entries are not in disk order

Command (m for help): x

Expert command (m for help): f


Done.

Expert command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)

7 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Syncing disks.

Once the partition table order is fixed, you’ll not get the “Partition table entries are not in disk order”
error message anymore.

# fdisk -l

Disk /dev/sda: 80.0 GB, 80026361856 bytes


255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System


/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 2662 3264 4843566 83 Linux
/dev/sda7 3265 5283 16217586 b W95 FAT32

7 Tweet 18 Like 13 > Add your comment

Linux provides several powerful administrative tools and utilities which


will help you to manage your systems effectively. If you don’t know what these tools are and how to
use them, you could be spending lot of time trying to perform even the basic administrative tasks.
The focus of this course is to help you understand system administration tools, which will help you to
become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

1. 50 Linux Sysadmin Tutorials Awk Introduction – 7 Awk Print Examples


2. 50 Most Frequently Used Linux Commands Advanced Sed Substitution Examples
(With Examples) 8 Essential Vim Editor Navigation
3. Top 25 Best Linux Performance Monitoring and Fundamentals
Debugging Tools 25 Most Frequently Used Linux IPTables
4. Mommy, I found it! – 15 Practical Linux Find Rules Examples
Command Examples Turbocharge PuTTY with 12 Powerful

8 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

5. Linux 101 Hacks 2nd Edition eBook Add-Ons

{ 14 comments… read them below or add one }

1 anurag rana September 14, 2010 at 2:50 am

thanxs ……

2 anurag rana September 14, 2010 at 2:52 am

sir , you provides very good tuts….i use to save every single page of your articles… thanxs
again..

3 Madharasan September 14, 2010 at 4:57 am

The “Warning Message” in this article reminds me of the mistake I did while teaching linux to a
group of students.

I was about to enter the command # mkfs.ext3 /dev/sda4


But unfortunately just when I have finished typing # mkfs.ext3 /dev/sda , I accidentely pressed
the enter key….

There ends the story of my LINUX OS of that machine and my session on Disk management.

Now a days, I first enter the device details and options, only then the command

Conclusion:
No Playing with linux, at times it is merciless

4 Chris F.A. Johnson September 14, 2010 at 1:43 pm

9 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

s/loose/lose/

5 DarkForce September 14, 2010 at 5:21 pm

Thanks for tip number 7.

6 Ramesh Natarajan September 14, 2010 at 8:51 pm

@Chris,

Thanks for pointing out the typo. It’s fixed.

7 jameslee September 15, 2010 at 9:35 am

thank u sir,its very useful

8 raralee September 15, 2010 at 11:07 am

good tips.
how to add new space on the existing partition using fdisk without losing data?

9 Sebas September 16, 2010 at 12:46 pm

Add new space w/o loosing data with fdisk….? i think that’s not posible.

10 madharasan September 17, 2010 at 1:48 am

@raralee

resizing a partition without losing data is not possible.


But if you have a plan already that you would be changing partition sizes, then please use LVM
( Logical Volume Manager )

11 alieblice June 27, 2011 at 3:50 pm

I have problem with fdisk command


when i tell it to make 80 megabyte drive with this input (( +80M)) it make 85 megabyte drive
and the drive is made by vmware player on ubutnu 10.04

any idea why this happen ?

12 Guus August 29, 2011 at 10:56 am

Thanks a lot!
After restoring a Windows installation to a new harddisk, i found that i had made a mistake with

10 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

the partition-order, causing a non-boot. After reordering the partitions, it boots again (although i
won’t use it, not my machine .

13 krishnadas.p.c July 22, 2012 at 6:30 am

It will be better if you add how to change the partition type too in this.

Change the partition type on the newly created partition from Linux to LVM (8e).

# fdisk /dev/sda
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Unknown)
Command (m for help): w

14 hanna November 21, 2012 at 6:54 am

i had grub15 error so i read that using fdisk will adjust order of sda. so i did fdisk -l then fdisk
/dev/sda then x f showed only two partitions and did w. now i am having problem that my
home is not loading system in root emergency mode. i can see my data after using ctrl D and
then home and then my data but i cant backup or copy my data to a usb drive plz tell what to
do after doing fdisk -l i can see all partitions i.e. 6 partiions

Leave a Comment

Name

E-mail

Website

Notify me of followup comments via e-mail

Previous post: 6 Perl File Handle Examples to Open, Read, and Write File

11 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Next post: VMWare ESXi 4: How to Add Virtual Hard Disk (from Datastore) to a VM Using vSphere
Client

Search

>Email >RSS >Twitter >Facebook

COURSE

Linux Sysadmin CentOS 6 Course - Master the Tools, Configure it Right, and be Lazy

EBOOKS

Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong
Foundation in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well

POPULAR POSTS

12 Amazing and Essential Linux Books To Enrich Your Brain and Library
50 UNIX / Linux Sysadmin Tutorials
50 Most Frequently Used UNIX / Linux Commands (With Examples)
How To Be Productive and Get Things Done Using GTD
30 Things To Do When you are Bored and have a Computer
Linux Directory Structure (File System Structure) Explained with Examples

12 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Linux Crontab: 15 Awesome Cron Job Examples


Get a Grip on the Grep! – 15 Practical Grep Command Examples
Unix LS Command: 15 Practical Examples
15 Examples To Master Linux Command Line History
Top 10 Open Source Bug Tracking System
Vi and Vim Macro Tutorial: How To Record and Play
Mommy, I found it! -- 15 Practical Linux Find Command Examples
15 Awesome Gmail Tips and Tricks
15 Awesome Google Search Tips and Tricks
RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
Can You Top This? 15 Practical Linux Top Command Examples
Top 5 Best System Monitoring Tools
Top 5 Best Linux OS Distributions
How To Monitor Remote Linux Host using Nagios 3.0
Awk Introduction Tutorial – 7 Awk Print Examples
How to Backup Linux? 15 rsync Command Examples
The Ultimate Wget Download Guide With 15 Awesome Examples
Top 5 Best Linux Text Editors
Packet Analyzer: 15 TCPDUMP Command Examples
The Ultimate Bash Array Tutorial with 15 Examples
3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
Unix Sed Tutorial: Advanced Sed Substitution Examples
UNIX / Linux: 10 Netstat Command Examples
The Ultimate Guide for Creating Strong Passwords
6 Steps to Secure Your Home Wireless Network
Turbocharge PuTTY with 12 Powerful Add-Ons

CATEGORIES

Linux
Vim
Sed
Awk
Bash
Nagios
OpenSSH
IPTables
Apache

13 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

MySQL
Perl
Google
Ubuntu
PostgreSQL
Hello World
C Programming
C++ Programming
DELL
Oracle
VMware

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to,


troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to
write articles that will either teach you or help you resolve a problem. Read more about
Ramesh Natarajan and the blog.

Support Us

Support this blog by purchasing one of my ebooks.

Bash 101 Hacks eBook

Sed and Awk 101 Hacks eBook

Vim 101 Hacks eBook

Nagios Core 3 eBook

Contact Us

14 of 15 07-Jun-13 12:58 AM
7 Linux fdisk Command Examples to Manage Hard Disk Partition http://www.thegeekstuff.com/2010/09/linux-fdisk/

Email Me : Use this Contact Form to get in touch me with your comments, questions or
suggestions about this site. You can also simply drop me a line to say hello!.

Follow us on Twitter

Become a fan on Facebook

Copyright © 2008–2013 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise

15 of 15 07-Jun-13 12:58 AM

You might also like