10+ mke2fs command examples in Linux [Cheat Sheet]

Introduction to mke2fs command

The mke2fs command helps to create an ext2/ext3/ext4 filesystem in Linux. mke2fs is part of the e2fsprogs package. The programs in the e2fsprogs package are useful for creating, checking, and maintaining ext2, ext3, and ext4 filesystems. Other utilities available in the e2fsprogs are tune2fs, dumpe2fs, and debugfs.

The mkfs command also creates a filesystem of different types in Linux. It is part of the util-linux package and calls mke2fs when used to create an ext2/ext3/ext4 filesystem.

Advertisement

 

Syntax to use mke2fs command

The syntax to use mke2fs command is as follows:

$ sudo mke2fs [option] device

mke2fs command can be dangerous as it can destroy all data on your device. It is a good idea to have a backup if you do not want to lose any data.

 

Different examples to use mke2fs command

1. Create a filesystem on  the device

You can build a filesystem on the specified device by using the mke2fs command. By default, the mke2fs command uses the filesystem type as defined in the /etc/mke2fs.conf.

$ sudo mke2fs /dev/sdc1

Sample Output:

mke2fs command to create a filesystem

 

2. Specify the block size in bytes

The -b option allows you to specify the size of blocks in bytes. The valid block-size values are 1024, 2048, and 4096 bytes per block.

$ sudo mke2fs -b 2048 /dev/sdc1

Sample Output:

Advertisement

create a filesystem with specific block size

 

3. Check for bad blocks and create a filesystem

A bad block is an area of the storage device that is completely damaged and no longer reliable for storing data.

The mke2fs command with -c option helps to check for bad blocks in the device before creating a filesystem.

$ sudo mke2fs -c /dev/sdc1 

Sample Output:

check for bad blocks before creating a filesystem

 

4. Create a filesystem with a volume label

The -L option can be used to set the volume label when creating a filesystem.

For example, this command sets the filesystem volume name as golinux for /dev/sdc1.

$ sudo mke2fs -L golinux /dev/sdc1

Sample Output:

Advertisement

create a filesystem with specific volume name

 

5. Specify the filesystem type

You can specify the filesystem type with -t option and create a specific filesystem.

The following example creates an ext4 filesystem on /dev/sdc1.

$ sudo mke2fs -t ext4 /dev/sdc1

Sample Output:

mke2fs command to create an ext4 filesystem

 

6. Specify the bytes/inode ratio

With -i option, you can specify the number of bytes per inode when creating a filesystem on a device.

$ sudo mke2fs -i 8192 /dev/sdc1

Sample Output:

specify bytes per inode ratio when creating filesystem

You can run the tune2fs command to view the inode information.

Advertisement
$ sudo tune2fs -l /dev/sdc1 | grep Inode

tune2fs command to display inode information

The larger the bytes-per-inode ratio, the fewer inodes will be created.

Note: Be careful when choosing the correct value as it is not possible to expand the number of inodes on a filesystem after it is created.

 

7. Create a filesystem with a specific inode size

mke2fs creates 256-byte inodes by default. You can use the -I option to specify the size of each inode in bytes when creating a filesystem.

$ sudo mke2fs -I 512 /dev/sdc1

Output:

create a filesystem with specific inode size

 

8. Create the filesystem with an ext3 journal

The mke2fs command with -j option creates the filesystem with an ext3 journal.

$ sudo mke2fs -j /dev/sdc1

Output:

Advertisement

mke2fs command to create an ext3 journal

 

9. Set the last mounted directory for the filesystem

The -M option is used to set the last mounted directory for the filesystem.

$ sudo mke2fs -M /mnt /dev/sdc1

Output:

set last mounted directory of a filesystem

 

10. Force to Create a Filesystem

The -F option can be used to force mke2fs to create a filesystem, even if the specified device is not a partition on a block special device, or the given parameters do not make sense.

$ sudo mke2fs -F /dev/sdc1

You must specify this option twice to create a filesystem even if the filesystem is in use or mounted. It is not recommended to create a filesystem on a mounted partition.

 

11. Test run a mke2fs command

You can use -n option to display what the mke2fs command would do when it is run. This option does not actually create a filesystem.

$ sudo mke2fs -n /dev/sdc1

Output:

Advertisement

test run mke2fs command

 

Conclusion

By this point, you should be familiar with how to use mke2fs command to create an ext2/ext3/ext4 filesystem in Linux. You have learned several ways to create a filesystem with different parameters. If you have any confusion, do not hesitate to ask us in the comment section.

 

What’s Next

15 tune2fs command examples in Linux [Cheat Sheet]
10 basic & powerful commands to check file system type in Linux/Unix

 

Further Reading

man page for mke2fs command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X