Format a Drive on Linux from the Command Line
In this article
Formatting a drive from the Linux command line is done with the mkfs command (short for “make filesystem”). You can wipe a drive and create a new filesystem (like ext4, NTFS, or FAT32) in a few minutes, as long as you know the device name and have root privileges. The quickest way is: sudo mkfs.ext4 /dev/sdX1 — but always double-check the device name first, because formatting erases everything on that partition.
What You'll Need
- A Linux system with a terminal (any distribution works).
- Root or sudo access (you’ll be prompted for a password).
- The drive you want to format, connected to the machine (internal or external).
- Basic knowledge of the device naming convention (e.g.,
/dev/sda,/dev/nvme0n1).
Step-by-Step
- Identify the drive. Run
lsblkin the terminal. This lists all block devices with their sizes and mount points. Look for the drive you want to format by its size (e.g., a 1TB drive shows as ~931.5G). Note the device name, like/dev/sdb(whole disk) or/dev/sdb1(partition). - Unmount the drive. If the drive is mounted, you must unmount it first. Use
sudo umount /dev/sdb1(replace with your partition). If it’s a whole disk with multiple partitions, unmount each one. To unmount all partitions on a disk, you can usesudo umount /dev/sdb*but this may fail if some are not mounted — that’s fine. - Decide the filesystem type. Common choices:
ext4— best for Linux system drives and most Linux-only storage.ntfs— for compatibility with Windows (read/write).fat32— for USB sticks and devices like cameras (but max file size 4GB).exfat— for large files on USB drives with cross-platform compatibility (installexfatprogsif needed).
- Format the partition (recommended) or the whole disk. If you have a partition like
/dev/sdb1, run:sudo mkfs.ext4 /dev/sdb1
Replaceext4with your chosen filesystem (e.g.,mkfs.ntfs,mkfs.fat -F32for FAT32,mkfs.exfat). If you want to format the whole disk (not recommended unless you intend to repartition), you can usesudo mkfs.ext4 /dev/sdb— but this will not create a partition table, which can cause issues. Better to format a partition. - If you need a new partition table first. If the drive is blank or has an old partition table you want to replace, use
sudo fdisk /dev/sdb(replace with your disk). Inside fdisk:- Type
gto create a new GPT partition table (orofor MBR/DOS). - Type
nto create a new partition, then press Enter to accept defaults (full disk). - Type
wto write changes and exit.
/dev/sdb1. Then format that partition with the mkfs command from step 4. - Type
- Verify the format. Run
lsblk -fto see the filesystem type and UUID. You should see the new filesystem label (e.g.,ext4) on the partition. - Mount and test (optional). To use the drive, mount it:
sudo mount /dev/sdb1 /mnt(or a folder of your choice). Then copy a test file to ensure it works.
Common Mistakes
- Formatting the wrong drive. This is the most dangerous mistake. Always triple-check the device name with
lsblkbefore running mkfs. A typo like/dev/sdainstead of/dev/sdbcan wipe your system drive. - Forgetting to unmount. If the drive is mounted, mkfs will refuse to run or may cause errors. Always unmount first.
- Using the wrong filesystem command. For FAT32, you must use
mkfs.fat -F32, notmkfs.vfat(though that also works on some systems). For NTFS, usemkfs.ntfs— not all distributions have it installed by default. - Formatting a whole disk instead of a partition. This can leave the drive without a partition table, making it unrecognizable to some tools. Use partitions unless you know what you’re doing.
Tips
- Always back up any important data before formatting — there is no undo.
- Use
lsblkwith-o NAME,SIZE,MOUNTPOINTfor a clearer view. - To label the drive, use the
-Loption:sudo mkfs.ext4 -L MyDrive /dev/sdb1. - For a quick check that you have the right drive, look at the model:
lsblk -d -o NAME,MODEL. - If you’re formatting an external USB drive, consider using
exfatfor compatibility with both Linux and Windows, and for files larger than 4GB.
Troubleshooting
If the format fails or the drive doesn’t work afterward, try these in order:
- “Device is busy” error. This means the drive is still mounted. Run
mount | grep /dev/sdb1to see where it’s mounted, then unmount withsudo umount /dev/sdb1. If it still says busy, usesudo lsof /dev/sdb1to find the process using it and close it. - “mkfs not found” error. The filesystem tool isn’t installed. Install it: for NTFS use
sudo apt install ntfs-3g(Debian/Ubuntu) orsudo dnf install ntfs-3g(Fedora). For exfat, installexfatprogs. For FAT32, installdosfstools. - Drive not showing up after format. Check
lsblk— if the partition isn’t there, you may have formatted the whole disk without a partition table. Recreate the partition table with fdisk (step 5) and then format the partition. - “Invalid argument” when formatting. This often happens if the device is a whole disk (e.g.,
/dev/sdb) instead of a partition. Use a partition like/dev/sdb1. - Can’t mount after formatting. Try mounting manually with
sudo mount /dev/sdb1 /mnt. If it fails, check the filesystem withsudo fsck /dev/sdb1— but only if it’s not mounted.
Frequently Asked Questions
Is there any way to recover data after formatting?
No, formatting erases the filesystem structure. Data recovery tools like testdisk or photorec might recover some files if you stop using the drive immediately, but it’s not guaranteed. Always back up.
What’s the difference between formatting a partition and a whole disk?
A partition is a section of a disk with its own filesystem. Formatting a whole disk without partitions creates a single filesystem that spans the entire disk, but many tools expect a partition table. It’s safer to create a partition table and then format a partition.
Can I format a drive while it’s mounted?
No, Linux will refuse to format a mounted filesystem. You must unmount it first.
How do I format a USB stick to FAT32?
Use sudo mkfs.fat -F32 /dev/sdX1 (replace with your partition). Ensure the partition is unmounted first.
What is the best filesystem for a Linux system drive?
ext4 is the default and most reliable for most Linux distributions. For servers, xfs is also common, but ext4 is fine for most users.
Do I need to format a new drive before using it?
Yes, a new drive typically has no filesystem. You need to create a partition table and then format a partition before you can mount and use it.