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 November 1, 2023 by Abhishek Sharma

In the realm of Linux system administration, the command-line interface is a powerful tool for managing and manipulating the operating system. Among the multitude of commands at your disposal, "fdisk" is a crucial utility that holds the key to partitioning your hard drive. Fdisk allows you to create, modify, and manage disk partitions, an essential task for organizing your storage space effectively. In this article, we’ll delve into the fdisk command in Linux, providing you with insights, examples, and FAQs to master this vital tool.

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.

In the world of Linux system administration, understanding and harnessing the power of the fdisk command is a critical skill. Properly partitioning your drives and managing your storage is a fundamental task that ensures your system runs smoothly and efficiently. With the knowledge gained from this article, you can confidently use fdisk to create, modify, and inspect partitions on your Linux machine.

As you continue to explore the diverse landscape of Linux commands, remember that practice makes perfect. Experiment with fdisk in a safe, non-production environment to get a feel for its capabilities and limitations. In doing so, you’ll gain the expertise required to navigate the complexities of disk management in Linux and make the most of your system’s resources.

Now that you’ve unlocked the power of fdisk, you’re well on your way to becoming a more proficient Linux system administrator. Keep honing your skills and exploring the vast world of Linux to master the command line and all its utilities. Happy partitioning!

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

FAQs related to fdisk Command in Linux with Examples.

Q1: Is fdisk the only command for managing partitions in Linux?
A1: No, there are other utilities like "parted" and "gparted" that offer partition management with a more user-friendly interface. However, fdisk remains a fundamental tool for administrators who prefer working with the command line.

Q2: Can I use fdisk to resize partitions?
A2: Fdisk is primarily used for creating and modifying partitions, not resizing them. To resize partitions, you may want to use tools like "resize2fs" for ext filesystems or "parted" for more complex resizing tasks.

Q3: Is there a risk of data loss when using fdisk?
A3: Yes, there is a risk of data loss when working with fdisk, especially if you make incorrect changes to your partitions. It’s crucial to back up your data and double-check your commands before applying any changes.

Q4: Can I use fdisk on any type of storage device?
A4: Fdisk is typically used for managing traditional hard drives (HDDs) and solid-state drives (SSDs). It may not be suitable for more specialized storage devices like USB flash drives or network-attached storage (NAS) devices.

Q5: How do I start fdisk in Linux?
A5: To use fdisk, open a terminal and type "sudo fdisk /dev/sdX" where "/dev/sdX" is the device you want to work with. Be cautious, as incorrect usage can lead to data loss.

Q6: What’s the difference between fdisk and gdisk?
A6: Fdisk is a legacy utility primarily used for managing MBR (Master Boot Record) partitions, while "gdisk" (or "gptfdisk") is used for GPT (GUID Partition Table) partitions, which are more modern and suitable for larger drives.

Leave a Reply

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