Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Using fdisk - Comprehensive Tutorial

Introduction to fdisk

fdisk is a powerful command-line utility used for disk partitioning in Unix-like operating systems. It allows users to create, modify, delete, and manage the partitions on a hard disk. This tutorial will guide you through the basics of using fdisk with detailed explanations and examples.

Prerequisites

Before we start, make sure you have the following:

  • A Unix-like operating system (Linux, BSD, etc.)
  • Root or sudo privileges to execute fdisk commands
  • Basic understanding of disk partitioning

Opening fdisk

To start using fdisk, you need to open a terminal and run the fdisk command followed by the disk you want to manage. For example:

sudo fdisk /dev/sda

This command opens fdisk for the primary hard drive (usually /dev/sda). Replace /dev/sda with the appropriate device name if you're working with a different disk.

Viewing the Partition Table

Once you have opened fdisk, you can view the current partition table by using the p command:

Command (m for help): p

This will display the current partitions on the disk.

Disk /dev/sda: 500 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    1026047      512000   83  Linux
/dev/sda2        1026048  976773167   487373560   8e  Linux LVM
                

Creating a New Partition

To create a new partition, use the n command:

Command (m for help): n

You will be prompted to choose the partition type (primary or extended), partition number, and the sectors for the partition. Follow the prompts to create the partition.

Deleting a Partition

To delete an existing partition, use the d command followed by the partition number:

Command (m for help): d Partition number (1-4): 2

This will delete the second partition on the disk.

Writing Changes to Disk

After making changes to the partitions, you need to write the changes to the disk using the w command:

Command (m for help): w

This will save the changes and exit fdisk.

Quitting Without Saving

If you want to quit fdisk without saving any changes, use the q command:

Command (m for help): q

Getting Help

If you need help or want to see a list of available commands, use the m command:

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)
                

Conclusion

fdisk is a versatile and powerful tool for disk management. With the commands and examples provided in this tutorial, you should be able to perform basic partitioning tasks. Always exercise caution when modifying disk partitions, as incorrect operations can lead to data loss.