Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Format a Drive on Linux from the Command Line

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Identify the drive. Run lsblk in 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).
  2. 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 use sudo umount /dev/sdb* but this may fail if some are not mounted — that’s fine.
  3. 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 (install exfatprogs if needed).
  4. Format the partition (recommended) or the whole disk. If you have a partition like /dev/sdb1, run:
    sudo mkfs.ext4 /dev/sdb1
    Replace ext4 with your chosen filesystem (e.g., mkfs.ntfs, mkfs.fat -F32 for FAT32, mkfs.exfat). If you want to format the whole disk (not recommended unless you intend to repartition), you can use sudo mkfs.ext4 /dev/sdb — but this will not create a partition table, which can cause issues. Better to format a partition.
  5. 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 g to create a new GPT partition table (or o for MBR/DOS).
    • Type n to create a new partition, then press Enter to accept defaults (full disk).
    • Type w to write changes and exit.
    After that, you’ll see a new partition like /dev/sdb1. Then format that partition with the mkfs command from step 4.
  6. Verify the format. Run lsblk -f to see the filesystem type and UUID. You should see the new filesystem label (e.g., ext4) on the partition.
  7. 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 lsblk before running mkfs. A typo like /dev/sda instead of /dev/sdb can 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, not mkfs.vfat (though that also works on some systems). For NTFS, use mkfs.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 lsblk with -o NAME,SIZE,MOUNTPOINT for a clearer view.
  • To label the drive, use the -L option: 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 exfat for 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:

  1. “Device is busy” error. This means the drive is still mounted. Run mount | grep /dev/sdb1 to see where it’s mounted, then unmount with sudo umount /dev/sdb1. If it still says busy, use sudo lsof /dev/sdb1 to find the process using it and close it.
  2. “mkfs not found” error. The filesystem tool isn’t installed. Install it: for NTFS use sudo apt install ntfs-3g (Debian/Ubuntu) or sudo dnf install ntfs-3g (Fedora). For exfat, install exfatprogs. For FAT32, install dosfstools.
  3. 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.
  4. “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.
  5. Can’t mount after formatting. Try mounting manually with sudo mount /dev/sdb1 /mnt. If it fails, check the filesystem with sudo 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.