How To: create filesystem with mkfs

The mkfs command available in UNIX and Linux operating systems is used to create file systems on various storage devices or partitions. It stands for “make filesystem”, and creating a file system is essentially an equivalent to what is popularly known as “formatting” a disk or a partition with a particular file system type (such as FAT32 or NTFS in Windows).

In other words you can use the mkfs command to format a storage device or a partition to a particular file system type, which can be ext2, ext3, ext4, FAT, NTFS, HFS, and others.

Basic usage of mkfs

This is its basic usage:

$ sudo mkfs -t type /dev/device

Where type should be replaced by the file system type such as ext3, and /dev/device by a device you want to format such as /dev/sdb1. The sudo command before mkfs just makes it run as a superuser or root, which is typically necessary when making file systems.

Here’s an example command:

$ sudo mkfs -t ext3 /dev/sdb1

This would format the device at /dev/sdb1 with an ext3 file system. Note that this will for sure delete all data you might have on that device!

Getting list of disks and paritions

If you’re not sure what device node (like /dev/sdb1) your partition or storage device is on you can run the sudo fdisk -l command to get a list that can help you determine which it is. If it is an USB stick or other external USB storage, for example, it will reside under a different letter than your internal disks.

So if your internal disk is /dev/sda (likely), and you don’t have two or more disks in your machine, then your external storage device will likely be at /dev/sdb. If you do have multiple storage devices built into your computer then they may be at /dev/sda and /dev/sdb respectively which would put any external device you connect at /dev/sdc. As for numbers, they simply represent the partition. So /dev/sdb1 is simply the first (even if only), partition on that device.

Moving back to making file sytems, you can also use shortcut commands that may be available for various file systems suck as mkfs.ext4 for ext4, mkfs.vfat for FAT, and so on. Then running a command like this..

$ sudo mkfs.ext4 /dev/sdb1

.. will have the same result as the previous example command, and will create an ext4 file system on /dev/sdb1, which in this example happens to be an USB flash drive.

Finally, if you receive a message telling you that the device is mounted and it will not make a file system on it you will need to unmount it using the umount command like this:

$ sudo umount /dev/sdb1

And then you can proceed with formatting as shown above.

See Also




Keep Learning

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets