Before you can use a disk or partition in Linux, it needs to be formatted with a specific filesystem. Common filesystems in Linux include ext4, Btrfs, and exFAT. Each of these filesystems serves different needs, and choosing the correct one depends on the intended use of the storage.

The mkfs command-line tool is commonly used for formatting disks and partitions in most Linux distributions. While there are graphical tools available, mkfs offers direct control over the formatting process. This tool is essential for preparing a disk or partition for use in a Linux system.

Understanding how to use mkfs is crucial for managing storage effectively in Linux. With this tool, you can format a disk or partition to the desired filesystem, ensuring the storage is ready for use according to your specific requirements.

Steps to format disk and partition in Linux:

  1. Open the terminal.
  2. List the available disks and partitions on your system.
    $ lsblk
    NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    loop0    7:0    0  55.4M  1 loop /snap/core18/19
    loop1    7:1    0  55.4M  1 loop /snap/core18/19
    loop2    7:2    0    51M  1 loop /snap/snap-stor
    loop3    7:3    0   219M  1 loop /snap/gnome-3-3
    loop4    7:4    0 217.9M  1 loop /snap/gnome-3-3
    loop5    7:5    0  31.1M  1 loop /snap/snapd/104
    loop6    7:6    0  62.1M  1 loop /snap/gtk-commo
    loop7    7:7    0  64.8M  1 loop /snap/gtk-commo
    loop8    7:8    0    51M  1 loop /snap/snap-stor
    loop9    7:9    0  31.1M  1 loop /snap/snapd/107
    sda      8:0    0    20G  0 disk 
    ├─sda1   8:1    0     1M  0 part 
    ├─sda2   8:2    0   513M  0 part /boot/efi
    └─sda3   8:3    0  19.5G  0 part /
    sdb      8:16   0    20G  0 disk 
    └─sdb1   8:17   0    20G  0 part 
    sr0     11:0    1  1024M  0 rom
  3. Identify the disk or partition you want to format.
  4. Unmount the disk or partition if it is mounted.
    $ sudo umount /dev/sdb1
    [sudo] password for user: 
    umount: /dev/sdb1: not mounted.

    Unmounting is necessary to avoid data loss during formatting.

  5. Check the supported filesystems.
    $ sudo mkfs -t
    mkfs.bfs     mkfs.ext4    mkfs.ntfs
    mkfs.cramfs  mkfs.fat     mkfs.vfat
    mkfs.ext2    mkfs.minix   
    mkfs.ext3    mkfs.msdos

    Type mkfs. and press <TAB> twice for the terminal to show possible autompleted commands.

    Install necessary package if your desired filestem is not listed or supported.

  6. Format the disk or partition to the desired filesystem.
    $ sudo mkfs.ext4 /dev/sdb1
    mke2fs 1.45.6 (20-Mar-2020)
    Creating filesystem with 5242624 4k blocks and 1310720 inodes
    Filesystem UUID: ccab0f8d-3b5b-4189-9da3-23c49159c318
    Superblock backups stored on blocks: 
    	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    	4096000
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    Replace ext4 with the desired filesystem, and adjust the device path as needed.

  7. Verify the formatting by checking the filesystem type.
    $ blkid /dev/sdb1 
    /dev/sdb1: UUID="ccab0f8d-3b5b-4189-9da3-23c49159c318" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c088a647-01"
  8. Mount the disk or partition if needed.
Discuss the article:

Comment anonymously. Login not required.