Create and Format a File System in Linux with mkfs
Learn how to identify a Linux partition, create a file system with mkfs, verify its UUID and type, mount it, and configure persistent mounting safely.
A Linux file system is the on-disk format and collection of data structures used to store and retrieve files and directories. It records file contents plus metadata such as ownership, permissions, timestamps, and the disk locations containing each file.
Creating a file system is commonly called formatting. Formatting is destructive: it replaces the existing file-system structures on the selected target and can make existing data inaccessible. Back up any data you need before running mkfs.
Partitioning, formatting, and mounting are different
These three operations occur at different stages:
- Partitioning divides a disk into defined regions, such as
/dev/sdb1. A partition is a region of a physical disk that can hold a file system. - Creating a file system writes structures such as inodes, allocation metadata, and superblocks into a partition or other block device.
- Mounting makes the file system accessible through a directory called a mount point, such as
/mnt/data.
A file system is not automatically accessible just because it has been created. It must be mounted before applications can use its files through the directory tree.
Identify the target device safely
Linux represents disks and partitions as block devices, usually under /dev. Device names vary according to the hardware or virtualization environment.
Do not assume that the first disk is always /dev/sda. USB, SATA, NVMe, virtual, and removable devices can receive different names between boots or between machines.
Inspect disks, file systems, and mounts
lsblk -fThe -f option displays useful file-system information, including device names, file-system types, labels, UUIDs, and mount points. Compare the output with the intended disk's capacity and partition layout.
findmntfindmnt lists file systems currently mounted in the directory tree. The target must not be mounted when you format it.
sudo blkidblkid reports recognized file-system types, labels, and UUIDs. Use all three commands before formatting, not just the success message from a later command.
Confirm that the selected partition is not:
- the active root file system or another operating-system file system;
- mounted at a directory;
- an active LVM physical volume, RAID member, encrypted container, or swap device;
- being used by an application or another storage layer.
Partition a disk before formatting
A partition is commonly created before the file system. Partition tables describe where partitions begin and end; the file system occupies the selected partition after it is created.
fdisk is an interactive command-line utility for viewing and modifying MBR or GPT partition tables. parted is another partitioning utility and is useful for interactive or scripted workflows.
sudo fdisk /dev/sdbFor an unused disk, the conceptual fdisk workflow is:
- Select the correct whole disk, such as
/dev/sdb, rather than guessing from its name. - Create a new partition and choose its size and partition type as appropriate.
- Review the proposed partition table before committing it.
- Write the partition table to disk, then quit.
After the table changes, ask the kernel to reread it:
sudo partprobe /dev/sdbThen confirm the resulting partition:
lsblk /dev/sdbThe new partition might be named /dev/sdb1. In normal use, mkfs should be run on that partition, not accidentally on the entire disk /dev/sdb. Formatting the whole disk may overwrite its partition table and all partitions.
mkfs command syntax
The generic syntax is:
sudo mkfs -t FILE_SYSTEM_TYPE DEVICE-t FILE_SYSTEM_TYPEselects the format, such asext3orext4.DEVICEis the block device to format, normally a confirmed partition such as/dev/sdb1.mkfsis a front end that invokes a file-system-specific program such asmkfs.ext4ormkfs.xfs.
Creating a file system changes disk metadata, so elevated privileges are normally required. The exact options available depend on the selected file-system implementation.
Create an ext file system
ext3 example
ext3 is an older journaling Linux file system. Journaling records intended metadata changes so recovery after a crash is generally more manageable than with a non-journaling format.
sudo mkfs -t ext3 /dev/sda3This demonstrates the generic syntax and the device-target pattern. Run it only after confirming that /dev/sda3 is the intended partition and is not mounted or in use.
Modern ext4 usage
ext4 is the widely used modern successor to ext3 and is a common general-purpose choice on Linux.
sudo mkfs -t ext4 /dev/sdb1The file-system-specific form is:
sudo mkfs.ext4 /dev/sdb1A label makes a file system easier to recognize in inspection output:
sudo mkfs.ext4 -L data /dev/sdb1Labels are descriptive identifiers; the file system also receives a UUID, which is normally more suitable for persistent mount configuration.
Understand mkfs output and metadata
Successful ext formatting output may report blocks, block groups, inode tables, superblocks, and a completion message. These are not merely progress details: they describe the initial layout of the new file system.
An inode records metadata such as ownership, permissions, timestamps, file type, and references to the blocks containing file data. It does not normally contain the file's content itself.
Check for bad blocks during creation
A bad block is a storage area that cannot reliably store or return data. The -c option requests a bad-block check before creation:
sudo mkfs -t ext4 -c /dev/sdb1A full scan can take considerable time, especially on a large disk. It is a format-time scan, not a complete hardware-health assessment. SMART diagnostics, where supported, provide broader device-health information. A bad-block scan also does not make failing hardware reliable; errors should prompt backup and replacement planning.
Verify the result after formatting
Do not rely only on a zero exit status or the final line of mkfs output. Confirm that the expected partition was formatted:
lsblk -f /dev/sdb1
sudo blkid /dev/sdb1Check that the detected type is ext4, the label is the expected value, and a UUID is present. For ext file systems, additional metadata can be inspected with:
sudo tune2fs -l /dev/sdb1tune2fs is intended for ext-family file systems. It can display the file-system label, UUID, block size, inode count, mount counts, and other superblock information. Use inspection tools appropriate to the actual file-system type.
Mount the new file system
Formatting creates the file system but does not expose it in the directory tree. Create a mount point and mount the partition:
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/dataVerify the specific mount:
findmnt /mnt/data
df -h /mnt/datafindmnt confirms which device is mounted at the path. df reports space usage for the mounted file system. These checks help detect a mount-point mistake, such as examining a directory that is still on the root file system.
Configure a persistent mount with UUID
Manual mounts normally last until shutdown or unmounting. To mount the file system automatically, add an entry to /etc/fstab. A UUID is a unique file-system identifier and is more stable than a device name that may change with hardware discovery order.
sudo blkid /dev/sdb1
sudoedit /etc/fstabAdd an entry using the actual UUID:
UUID=the-filesystem-uuid /mnt/data ext4 defaults 0 2The fields specify the UUID, mount point, file-system type, mount options, dump setting, and file-system check order. Ensure that /mnt/data exists and that the type matches the value reported by blkid.
Validate the configuration without rebooting:
sudo mount -a
findmnt /mnt/dataIf mount -a reports an error, correct the entry before rebooting. A malformed /etc/fstab entry can prevent the intended file system from mounting during startup.
Troubleshooting
The wrong target may be selected
When the device name is uncertain or the device contains an unexpected file system, stop. Run lsblk -f and findmnt, then compare capacity, partition layout, labels, UUIDs, and mount points with the intended storage. Use the confirmed partition path rather than guessing from disk order.
mkfs reports that the device is busy
Check findmnt and lsblk -f. Also determine whether the device belongs to LVM, RAID, encryption, or swap. Unmount the intended file system only when it is safe, and do not force formatting of an active system-storage component.
The new partition does not appear
Review the partition table with fdisk or lsblk. If the kernel has not reread the table, run:
sudo partprobe /dev/sdbRe-scan as appropriate, or safely reboot after confirming that no other storage changes are pending.
Mount reports an unknown or wrong file-system type
Run blkid on the partition and compare the detected type with the mount configuration. Confirm that the required file-system support tools and kernel support are installed. Reformat only if the target is confirmed and data loss is acceptable.
A persistent mount fails
Check the UUID with blkid, the mount-point directory, the file-system type, and the field order in /etc/fstab. Correct the UUID-based entry and run sudo mount -a successfully before rebooting.
The file system reports no space unexpectedly
Check block usage:
df -h /mnt/dataFor ext file systems, also check inode usage:
df -i /mnt/dataIf blocks are exhausted, remove unneeded data or increase capacity. If inodes are exhausted, the workload may contain too many small files for the file system's inode layout; a file system recreated with suitable inode density may be required.
Bad-block checking finds errors
Treat scan errors as a possible hardware-reliability problem. Back up accessible data and review SMART-capable health information when applicable. Do not rely solely on marking bad blocks; plan to replace failing storage.
Exam-relevant notes
mkfscreates a file system; it does not partition a disk or mount the result.- The generic syntax is
mkfs -t FILE_SYSTEM_TYPE DEVICE. mkfs.ext4is the file-system-specific equivalent for ext4.- Formatting the selected target destroys existing data.
- Use
lsblk -f,findmnt, andblkidto verify identity, file-system metadata, and mount status. - Use UUIDs in
/etc/fstabfor stable persistent mounts, and test entries withmount -a. - In ext file systems, inode availability can limit the number of files even when ordinary block space remains.
Next steps
For related administration tasks, continue with Linux command-line topics, review how to show the full path of shell commands, and learn more about Bourne Again Shell Bash.