mke2fs

disk managementLinux
The mke2fs command is one of the most frequently used commands in Linux/Unix-like operating systems. mke2fs Create an ext2/ext3/ext4 filesystem

Quick Reference

Command Name:

mke2fs

Category:

disk management

Platform:

Linux

Basic Usage:

mke2fs [options] [arguments]

Common Use Cases

    Syntax

    mke2fs [options] device [blocks-count]

    Options

    Option Description
    -b, --block-size=SIZE Block size in bytes (1024, 2048, or 4096)
    -c, --check Check for bad blocks before creating the filesystem
    -d, --root-directory=DIR Copy the contents of the specified directory to the root of the new filesystem
    -E, --extended-options=opts Set extended options for the filesystem
    -F, --force Force mke2fs to create a filesystem even if the specified device is not a block device, or if it's mounted
    -g, --blocks-per-group=BLOCKS Number of blocks in a block group
    -G, --group=blocks-per-group Number of blocks per group
    -i, --bytes-per-inode=BYTES Bytes per inode
    -I, --inode-size=SIZE Size of each inode in bytes
    -j, --journal Create an ext3 journal
    -J, --journal-options=opts Journal options for the filesystem
    -l, --bad-blocks-file=FILE Add the specified blocks listed in the FILE to the bad blocks list
    -L, --volume-label=LABEL Set the volume label for the filesystem
    -m, --reserved-blocks-percentage=PERCENT Percentage of blocks reserved for the super-user
    -M, --mounted-dir=DIR The directory where the filesystem will be mounted
    -n, --dry-run Don't actually create a filesystem, just show what would be done
    -N, --inodes=NUMBER Specify the number of inodes to create
    -O, --feature[,...], --features=feature[,...] Create a filesystem with the given features
    -q, --quiet Quiet execution (minimal output)
    -r, --fs-revision-level=REV Set the filesystem revision
    -S, --super-only Write superblock and group descriptors only
    -t, --type=fs-type Specify the filesystem type (ext2, ext3, or ext4)
    -T, --usage-type=type Specify how the filesystem will be used (e.g., news, largefile, etc.)
    -U, --uuid=UUID Set the UUID for the filesystem
    -v, --verbose Verbose execution

    Examples

    How to Use These Examples

    The examples below show common ways to use the mke2fs command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    mke2fs /dev/sdb1
    Create an ext2 filesystem on the /dev/sdb1 partition.
    mke2fs -t ext4 /dev/sdc1
    Create an ext4 filesystem on the /dev/sdc1 partition.
    # Advanced Examples Advanced
    mke2fs -t ext4 -L "BACKUP" /dev/sdd1 Create an ext4 filesystem with the label "BACKUP". mke2fs -t ext4 -j /dev/sde1 Create an ext3 filesystem (ext2 with journaling). mke2fs -t ext4 -b 4096 /dev/sdf1 Create an ext4 filesystem with a 4096-byte block size. mke2fs -t ext4 -m 2 /dev/sdg1 Reserve 2% of filesystem blocks for the super-user. mke2fs -t ext4 -N 2000000 /dev/sdh1 Create an ext4 filesystem with space for 2 million inodes. mke2fs -t ext4 -O dir_index,extent /dev/sdi1 Create an ext4 filesystem with specific features enabled. mke2fs -t ext4 -E lazy_itable_init=1 /dev/sdj1 Create an ext4 filesystem with lazy inode table initialization. mke2fs -t ext4 -T largefile /dev/sdk1 Create an ext4 filesystem optimized for large files.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'mke2fs' command is a powerful utility for creating ext2, ext3, and ext4 filesystems on Linux. These filesystems are the standard native filesystems for Linux systems, with ext4 being the most advanced and commonly used version. The command allows for extensive customization of filesystem parameters to optimize for specific use cases. Key features of the mke2fs command: 1. Filesystem Creation: mke2fs formats a specified device (typically a disk partition) with one of the ext family filesystems, preparing it for use in a Linux system. 2. Filesystem Type Selection: With the -t option, you can explicitly choose which ext filesystem variant to create (ext2, ext3, or ext4), each offering different features and performance characteristics. 3. Journaling Support: For ext3 and ext4 filesystems, mke2fs creates a journal that improves filesystem reliability by tracking changes before they are committed, helping recovery after system crashes. 4. Block and Inode Customization: The command provides extensive options to customize the block size, inode size, and inode ratio, allowing optimization for specific workloads (many small files vs. fewer large files). 5. Reserved Space Management: By default, mke2fs reserves 5% of the filesystem blocks for the super-user, which can be adjusted with the -m option to ensure system functionality even when disk space is low. 6. Feature Selection: Modern ext4 filesystems support numerous features like extents, directory indexing, and delayed allocation, which can be selectively enabled or disabled using the -O option. 7. Bad Block Handling: The command can scan for and mark bad blocks on the storage medium, preventing data from being stored in potentially unreliable sectors. Common use cases for mke2fs include: - Preparing new disk partitions for use in Linux systems - Reformatting existing partitions with different filesystem parameters - Creating specialized filesystems optimized for specific applications (databases, mail servers, etc.) - Setting up filesystems with custom labels or UUIDs for consistent mounting - Creating filesystems with special performance or reliability characteristics It's important to note that mke2fs completely erases any existing data on the target device. Always ensure you have backups of important data before running this command on a device that contains information you want to keep. Also, while mke2fs is the primary command for creating ext filesystems, it's worth mentioning that 'mkfs.ext2', 'mkfs.ext3', and 'mkfs.ext4' are essentially front-ends to mke2fs that simplify the process of creating specific filesystem types. The ext4 filesystem, which can be created with mke2fs -t ext4, is the most advanced of the ext family and offers features like larger filesystem support (up to 1 exbibyte), improved performance, reduced fragmentation, delayed allocation, and faster filesystem checking, making it the preferred choice for most modern Linux installations.

    Related Commands

    These commands are frequently used alongside mke2fs or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mke2fs command works in different scenarios.

    $ mke2fs
    View All Commands