When you install a brand-new hard drive or SSD into a Linux server, or when you use fdisk to carve a drive into multiple partitions, the new storage space is completely unusable. A raw partition is simply empty magnetic or flash storage; the operating system has no idea how to write files to it or organize directories. Before you can save a single byte of data to a new partition, you must build a “file system” on top of it. In Linux, you achieve this using the mkfs (make file system) command.
Understanding File Systems
A file system is essentially an index and structural map that tells the Linux kernel exactly how data is laid out on the physical disk. Different file systems offer different features (like journaling for crash recovery or support for massive file sizes). The most common file systems in modern Linux distributions are ext4, xfs, and btrfs.
How to Format a Partition
Before running mkfs, you must know the exact device block name of the partition you want to format (e.g., /dev/sdb1). Warning: Running the mkfs command will instantly and permanently obliterate any existing data on that partition. Always double-check your drive letters using lsblk -f before proceeding.
The mkfs command acts as a universal frontend. To use it, you must append a period and the name of the specific file system you wish to create.
Creating an ext4 File System
The ext4 file system is the reliable, rock-solid default for almost all Ubuntu and Debian-based systems.
sudo mkfs.ext4 /dev/sdb1
When you run this command, Linux will output a block of text detailing the creation of the inode tables, block groups, and the journal. Once it completes, the partition is fully formatted.
Creating an XFS File System
If you are managing large enterprise servers running Red Hat Enterprise Linux (RHEL) or handling massive files (like video editing drives), the xfs file system is often the preferred choice due to its high performance.
sudo mkfs.xfs /dev/sdb1
Note: If you are trying to overwrite a partition that already contains an older file system, mkfs.xfs might refuse to run to protect you. You can force it to overwrite the old data by adding the -f (force) flag:
sudo mkfs.xfs -f /dev/sdb1
Mounting the Formatted Drive
Once mkfs finishes its job, the drive is formatted but still not accessible. You must attach it to your directory tree using the mount command.
sudo mkdir /mnt/new_drive
sudo mount /dev/sdb1 /mnt/new_drive
You can now navigate to /mnt/new_drive and begin saving files to your freshly formatted partition.