Home > Package & System Management > mkfs

mkfs: File System Creation Tool

The mkfs command is used in Linux to create (format) file systems on disk partitions. With this tool, you can create new partitions and build a logical structure where data can be stored. It supports various file system types (ext4, XFS, FAT, etc.).

Overview

mkfs (make file system) is a command used to create a file system on a disk partition or volume. After creating a partition (e.g., using `fdisk` or `parted`), you must format that partition with a file system to store files and directories. `mkfs` actually acts as a frontend that calls dedicated commands for creating specific file systems, such as `mkfs.ext4` or `mkfs.xfs`. Therefore, `mkfs -t ext4` and `mkfs.ext4` perform the same function.

Key Features

Key features of the mkfs command include:

  • Supports various file system types.
  • Creates a logical structure on new partitions for storing files.
  • Outputs warning messages to prevent accidental overwriting of important data.
  • After partitioning with `fdisk` or `parted`, it must be formatted with `mkfs` to be usable.

File System Types

Major file system types supported by mkfs.

  • ext4: The most widely used Linux file system. Offers excellent performance and stability.
  • XFS: A high-performance file system specialized for large-scale file systems. It is the default file system for CentOS/RHEL 7 and later.
  • FAT: Primarily used for removable devices like USB drives. Compatible with various operating systems including Linux, Windows, and macOS.

Key Options

The mkfs command specifies the file system type using the `-t` option, and other options are directly passed to the specific file system command (e.g., `mkfs.ext4`).

1) Execution Options

Generated command:

Try combining the commands.

Description:

`mkfs` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Explore the functionality of the mkfs command through various usage examples. **All examples will delete data on the target partition, so exercise caution when executing.**

Create an ext4 File System

sudo mkfs -t ext4 /dev/sdb1

Creates the most common `ext4` file system on the `/dev/sdb1` partition.

Create an XFS File System

sudo mkfs.xfs /dev/sdb1

Creates an `XFS` file system suitable for large disks. It is used as the default format in CentOS/RHEL.

Create a FAT32 File System

sudo mkfs.fat -F 32 /dev/sdb1

Creates a `FAT32` file system with good compatibility for removable storage devices like USB drives.

Create File System and Assign Label Simultaneously

sudo mkfs.ext4 -L "Data_Partition" /dev/sdb1

Assigns a `LABEL` while creating the file system, making it easier to identify the partition later.

Installation

mkfs is part of the `util-linux` package and is included by default in most Linux distributions. However, specific file system tools like `mkfs.xfs` might require separate package installations.

Install XFS Tools (Debian/Ubuntu)

sudo apt update
sudo apt install -y xfsprogs

Install XFS Tools (RHEL/CentOS)

sudo dnf install -y xfsprogs

Tips & Cautions

Important points to consider when using the mkfs command.

Tips

  • The mkfs command requires **`sudo` privileges**. Running without root privileges will result in a `Permission denied` error.
  • Before running mkfs, you must accurately verify the device name of the partition to be formatted (e.g., `/dev/sdb1`) using `fdisk -l` or `lsblk`. Formatting the wrong partition will lead to data loss.
  • After creating a file system, you must mount the partition to a directory using the `mount` command to make it usable.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands