Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

fdisk Command in Linux with Examples

Last Updated on June 19, 2024 by Abhishek Sharma

The fdisk command in Linux is a powerful tool used for disk partitioning. It allows system administrators to create, delete, resize, and manage partitions on a hard drive. This command-line utility is essential for configuring storage devices, preparing disks for new installations, and managing existing partitions. Understanding how to use fdisk effectively can help you optimize disk space utilization and ensure that your Linux system runs smoothly.

What is Fdisk Command in Linux with Examples?

Managing disk partitions is a fundamental skill for Linux system administrators. The fdisk command is a powerful tool for partitioning and managing disk drives on a Linux system. In this article, we’ll explore the basics of using fdisk with practical examples to guide you through the process.

Understanding fdisk

fdisk is a command-line utility used for partitioning hard drives. It allows you to create, modify, and delete partitions on your storage devices. You can use fdisk on both traditional hard drives (HDDs) and solid-state drives (SSDs).

Using fdisk to Partition Drives

Example 1: Launching fdisk
To begin, open a terminal and run fdisk as the superuser (or using sudo) followed by the device you want to partition. For instance:

sudo fdisk /dev/sdX

Replace /dev/sdX with the actual device name (e.g., /dev/sda) you want to work on.

Example 2: Creating a New Partition

  1. After launching fdisk, press n to create a new partition.
  2. Choose whether you want a primary (type p) or extended (type e) partition.
  3. Specify the partition number (e.g., 1).
  4. Set the starting and ending sectors or press Enter to use the default values to use the entire disk.
  5. To save changes, press w.

Example 3: Deleting a Partition

  1. In fdisk, press d to delete a partition.
  2. Choose the partition number you want to delete.
  3. Confirm the action by typing yes.
  4. To save changes, press w.
  5. Modifying Partitions

Example 4: Changing the Partition Type
You can change the type of an existing partition by using t.

  1. In fdisk, press t to change the partition type.
  2. Select the partition number.
  3. Enter the hex code for the desired partition type (e.g., 83 for Linux).
  4. To save changes, press w.

Example 5: Resizing a Partition
To resize a partition, you need to delete it and then create a new one with the desired size. Be sure to back up your data before attempting this.

Displaying Partition Information
Example 6: Viewing Partition Information
To view the partitions on a disk, use p.

  • In fdisk, press p.
  • This command will display a list of all partitions on the selected disk.

Saving Changes and Exiting fdisk
Example 7: Saving Changes and Exiting
After making the desired changes, use w to save the changes and exit fdisk.

Conclusion:
The fdisk command is a versatile and essential tool for disk partitioning and management in Linux. Whether you are setting up a new system, resizing partitions, or preparing disks for different file systems, fdisk provides the functionality needed to manage your storage devices effectively. By mastering the use of fdisk, you can ensure that your Linux system’s storage is optimized and configured correctly, contributing to overall system stability and performance.

FAQ (Frequently Asked Questions) Related to fdisk Command in Linux with Examples

FAQs related to fdisk Command in Linux with Examples.

Q1: What is the fdisk command used for in Linux?
The fdisk command is used for disk partitioning in Linux. It allows you to create, delete, resize, and manage partitions on a hard drive

Q2: How do I list all partitions on a disk using fdisk?
You can list all partitions on a disk using the -l option. For example, to list partitions on /dev/sda, you would run:

sudo fdisk -l /dev/sda

Q3: How do I start fdisk to partition a specific disk?
To start fdisk for a specific disk, simply run fdisk followed by the disk’s device name. For example:

sudo fdisk /dev/sda

Q4: What are the common commands within the fdisk utility?
Some common fdisk commands include:

  • m: Display the help menu.
  • p: Print the partition table.
  • n: Create a new partition.
  • d: Delete a partition.
  • t: Change a partition’s type.
  • w: Write the changes to disk and exit.
  • q: Quit without saving changes.

Q5: How can I resize a partition using fdisk?
fdisk does not directly support resizing partitions. To resize a partition, you typically need to delete the existing partition and create a new one with the desired size, ensuring that you do not format it to preserve the data. Alternatively, you can use tools like parted or gparted for resizing.

Leave a Reply

Your email address will not be published. Required fields are marked *