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.
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.
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:
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:
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:
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:
5. Create file system with specific 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:
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:
You can run the tune2fs command to view the inode information.
$ sudo tune2fs -l /dev/sdc1 | grep Inode
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:
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:
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:
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:
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