How to format disk in Linux

Formatting a disk will get it ready for use as a storage device on your Linux system. The process involves partitioning the disk, adding a file system to the partition (this is the “formatting” part), and then mounting the partition to some path where you plan to access it from. This might sound complex or like a lot of steps, but it really only takes a few minutes.

This process will wipe all the data from your hard disk and get it ready for use under Linux or another system. In this tutorial, we will cover the step by step instructions to format a hard drive or solid state drive on a Linux system. We will show the steps for both command line and GUI methods, so you can follow along with set of instructions you are most comfortable with. Let’s get started.

In this tutorial you will learn:

  • How to format a disk drive in Linux via GUI
  • How to format a disk drive in Linux via command line
How to format disk in Linux
How to format disk in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software gdisk, gparted
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to format a disk on Linux via command line



DID YOU KNOW?
If your intention is to use the hard drive as a primary disk for a Linux installation, then no partitioning is required as any decent Linux operating system will do the job for you during the installation process.

Open a terminal and type the following commands to format your hard disk drive. Here we will be using the gdisk command line tool, which should already be installed by default on all major Linux distros.

  1. First, let’s figure out how to identify the disk drive we wish to format. The name should start with /dev/sd and then a letter. Type the following command in terminal to see:
    $ sudo fdisk -l
    
    Find your device name in the fdisk output
    Find your device name in the fdisk output
  2. In our example, the disk drive has been assigned the /dev/sdb device path. To create a new partition on the empty disk, we will provide it as an argument to gdisk:
    $ sudo gdisk /dev/sdX
    
  3. The gdisk utility awaits our commands. We would like to create a new partition, so we press “n”.
    Command (? for help): n
    
  4. This will be the first partition that we are creating on this disk, so the answer to the next question is “1”.
    Partition number (1-128, default 1): 1
    
  5. The next questions are about first and last sector, which will determine the actual size of the partition. In our example we are creating a single partition that will cover the disk, and default values are first partition, first available sector to start, and last sector to end with, which is just what we need. So we will accept the defaults for these questions by simply pressing the Enter key.
    First sector (34-6291455966, default = 2048) or {+-}size{KMGTP}: 
    Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}:
    
  6. The next questions asks us what kind of file system this partition will be for. We need to enter a hex code that corresponds to our selection. However, the default response is ‘Linux filesystem’ which is exactly what we need. We will once again press the Enter key to accept this default value.


    Current type is 8300 (Linux filesystem)
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'
    
  7. The partition is now complete, but as the utility points out on start, the changes are in-memory only until we write them out to the disk. This is on purpose and the warning is in place for a good reason: by writing out the changes to the disk, we destroy anything that resided on the sector range we cover with our new partition. We are sure there will be no data loss, so we write the changes to disk with the w command:
    Command (? for help): w
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/sdb.
    The operation has completed successfully.
    

    You will also need to reply Y to the confirmation prompt to verify that you indeed want to write these changes to the disk, and overwrite existing data.

    We are finished partitioning the disk drive with the gdisk utility
    We are finished partitioning the disk drive with the gdisk utility
  8. Since our block device is /dev/sdb, and we just created partition number 1 on the disk, that means our new partition is accessible under the path /dev/sdb1. Next, we still need to add a file system to our hard disk. Use the mkfs command to format the disk with any file system you would like. In this example, we are using ext4, which is the recommended file system for new HDDs and SSDs on Linux:
    $ sudo mkfs -t ext4 /dev/sdX1
    
  9. We will now use the mount command to mount the newly formatted partition on our system. We will mount our disk drive to the /media/disk directory.
    $ sudo mkdir -p /media/disk
    $ sudo mount /dev/sdb1 /media/disk
    

That is all there is to it. You can now access your newly formatted drive under the /mount/disk directory or where ever you decided to mount it. To make the drive get mounted automatically, see our guide on configuring the /etc/fstab file.

How to format a disk on Linux via GUI

There are many GUI programs which can also be used to format a hard disk on Linux. In this tutorial, we will focus on using gparted, which may or may not already be installed by default on your system.




You can use the appropriate command below to install gparted with your system’s package manager.

To install gparted on Ubuntu, Debian, and Linux Mint:

$ sudo apt install gparted

To install gparted on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install gparted

To install gparted on Arch Linux and Manjaro:

$ sudo pacman -S gparted

After it is installed, follow the steps below to use gparted to format a disk on Linux:

  1. Get started by searching for the gparted application in your desktop envrionment’s app launcher. You will be prompted for the root password upon opening the program.
    Search for and open the gparted app
    Search for and open the gparted app
    You must supply your root password in order to use gparted
    You must supply your root password in order to use gparted
  2. The first thing we need to do is select the correct device that we will be working with from the upper right corner. On our test system, this would be /dev/sdb as indicated in the screenshot below.

    First select the correct hard disk to work with in gparted
    First select the correct hard disk to work with in gparted



  3. As you can see, your disk currently has no partitions. Let’s add one by going to Device > Create Partition Table.
    Select to create a new partition table from the Device menu
    Select to create a new partition table from the Device menu
  4. Next, select the type of partition you would like to create. We will by sticking with gpt in our tutorial but feel free to pick something else. GPT is recommended for hard disks greater than 2 TB in size.
    Select partition type and confirm that these changes will erase current disk data
    Select partition type and confirm that these changes will erase current disk data
  5. Now that the partition table has been created, we can move forward with adding a new partition to the hard disk. This option can be found by heading to Partition > New.
    Select to create a new partition
    Select to create a new partition
  6. On this menu, we get to select the size of our new partition. Rather than working with exact values, feel free to use the sliders with your mouse in order to configure the size that you want. For our example, we will simply make one partition that spans the entire size of the hard disk. Click ‘Add’ when ready to proceed.
    Select size of partition and optionally a name
    Select size of partition and optionally a name
    NOTE
    ext4 is the recommended file system to use on Linux systems, unless you have a special reason to format the disk with some other kind.
  7. The changes are not yet written to the disk, but we can see in the preview pane the configurations that we are about to apply. Once you are ready, click on Edit > Apply All Operations.
    Applying the changes to disk
    Applying the changes to disk
  8. Afterwards, you can open your system’s GUI Disks application, select the partition we created earlier, and then mount the partition. Be sure to choose a path where you would like the partition to be accessible from.

    Mounting the partition via Disks GUI application
    Mounting the partition via Disks GUI application



Closing Thoughts

In this tutorial, we saw how to partition a hard drive from command line and GUI on a Linux system. Managing partitions is a dangerous task that should be performed with the utmost caution. The command line and GUI both prove as viable methods for managing partitions, especially in the case of parted and gparted, which are closely related tools.



Comments and Discussions
Linux Forum