Build a Linux Filesystem Safely with mkfs

mkfs hands off to the real filesystem builder, and picking the wrong target is one of the fastest ways to lose a partition. You will finish with a repeatable way to choose a builder, rehearse the command, and format a deliberately selected target. The examples use the installed mkfs frontend from util-linux 2.41.3.

Formatting destroys the existing filesystem on the target, so this guide uses a dry run before the destructive step and checks the target at each stage. Allow about fifteen minutes, plus the time needed to confirm your storage layout. You need a shell and the util-linux package. The real formatting command normally needs elevated privileges. Do not run it until you have positively identified the device or file and confirmed it holds nothing you need.

1. Check the installed frontend

Start with read-only checks. These do not need sudo:

$ command -v mkfs
/usr/sbin/mkfs
$ mkfs --version
mkfs from util-linux 2.41.3
$ mkfs --help

The local manual describes mkfs as a frontend, not the filesystem implementation itself. It searches your PATH for a program named mkfs.<type> and passes filesystem-specific arguments to it. The manual's package text says util-linux 2.39.3, while the executable installed here reports 2.41.3: when behaviour matters, trust the output from your own command, not the manual's printed version.

Checkpoint: identify the exact target before writing any formatting command. A device such as /dev/sdb1 is not interchangeable with its whole disk, /dev/sdb. If you have not established that distinction, stop here.

2. Inspect storage without changing it

Use your normal storage inventory tools to check names, sizes and current filesystem signatures. This is an ordinary read-only step:

$ lsblk -o NAME,PATH,SIZE,FSTYPE,LABEL,MOUNTPOINTS
$ findmnt

Replace /dev/sdX1 in the later examples with the real target only after comparing its path and size with a trusted inventory. A mounted filesystem is a particularly strong reason to pause: unmounting it can interrupt services and is outside this guide, so arrange that change deliberately before formatting.

Do not infer a target from a familiar-looking label, an order in lsblk, or a copied command from another machine. Device names can change between boots and hardware layouts.

3. Select the filesystem type explicitly

Choose a filesystem builder with -t. For example, ext4 makes the frontend search for mkfs.ext4:

$ mkfs -t ext4 -VV /dev/null
mkfs from util-linux 2.41.3
mkfs.ext4 /dev/null

The repeated -V is intentional. One -V requests verbose operation; more than one requests a dry run, so no filesystem-specific command runs. The output shows which builder the frontend would invoke. That harmless /dev/null target is only a rehearsal, never treat it as a formatted filesystem.

If you omit -t, the local mkfs(8) manual says the current default is ext2. Do not rely on that default in a script or a destructive command. Naming the intended type makes review easier and stops a changed default from silently altering the result.

4. Rehearse the real target

Set a shell variable to the target you have already checked, then run the dry run. This command is still non-destructive:

$ TARGET=/dev/sdX1
$ mkfs -t ext4 -VV "$TARGET"
mkfs from util-linux 2.41.3
mkfs.ext4 /dev/sdX1
$ printf 'dry-run status: %s\n' "$?"
dry-run status: 0

Expect the builder name and target path in the output; the exact line can include additional filesystem-specific options if you supply them. A status of 0 means the frontend completed its dry-run path. It does not prove the target is safe or that the builder stays installed for every future invocation.

Checkpoint: compare the printed target character by character with the storage inventory. If the path, type or builder is not exactly what you intended, do not proceed.

5. Format the target

Warning: the next command changes the target and normally needs root privileges. It destroys the filesystem metadata and makes existing data difficult or impossible to recover.

$ sudo mkfs -t ext4 "$TARGET"

Keep generic frontend options before filesystem-specific options. Any builder options must be supported by the selected mkfs.<type> program, so read that builder's manual before adding them. Do not paste an option string from a different filesystem.

A successful exit status is the first checkpoint:

$ printf 'format status: %s\n' "$?"
format status: 0

If the command fails, do not repeatedly rerun it. Keep the error text, check the target, and read the filesystem-specific manual. The frontend returns status 0 on success and 1 on failure according to mkfs(8).

6. Verify the result before using it

Use read-only inspection to confirm the target now reports the intended filesystem:

$ lsblk -f "$TARGET"
$ sudo blkid "$TARGET"

The output should identify an ext4 filesystem on the exact target. Labels, UUIDs and other fields depend on the builder and its options, so do not treat a particular UUID or label as a universal expected value. mkfs does not mount the filesystem; that is a separate operational decision.

If you formatted the wrong target, stop using it immediately. There is no general undo command for filesystem creation. Recovery may need backups or specialist data-recovery work. Do not create another filesystem on the same target while you investigate.

Common failure points

Done means