Ext2 File System Programs
e2fsprogs provides essential utilities for managing ext2, ext3, and ext4 filesystems (which are used by many Linux distros), including tools like e2fsck for checking, resize2fs for resizing, badblocks for scanning bad sectors, and debugfs for debugging.
This guide will go over the numerous tools this software suite has to offer, basic and advanced filesystem and recovery options.
Information
e2fsprogsis one of the preinstalled packages for our r/Techsupport Rescue Media. If you are using this live image, you can skip the installation section.
How to install e2fsprogsRefer below for installation instructions if you are not using our live image or want to install it on your own system.
Installing e2fsprogs
Debian / Ubuntu / Linux Mint:
Terminal window sudo apt update && sudo apt install e2fsprogsFedora / RHEL / CentOS / AlmaLinux:
Terminal window sudo dnf install e2fsprogsArch / Manjaro / CachyOS:
Terminal window sudo pacman -Syu e2fsprogs
The rest of the guide will be going over the different methods and tools available. Note, it is best to have a basic understanding of what stuff like /dev/sdb and /dev/sdb1 represent. A guide to understanding it can be found here.
WarningSome of these operations are DESTRUCTIVE (i.e. they will delete data). Double check the comments before running the commands.
Basic Repair Operations
Section titled “Basic Repair Operations”Check and Repair Filesystem (e2fsck)
Section titled “Check and Repair Filesystem (e2fsck)”Basic automatic repair:
# Unmount first (REQUIRED)sudo umount /dev/sdb1
# Automatic repair (answer yes to all)sudo e2fsck -y /dev/sdb1
# Force check even if filesystem appears cleansudo e2fsck -fy /dev/sdb1Interactive repair:
# Manual decisions for each errorsudo e2fsck /dev/sdb1
# With verbose outputsudo e2fsck -v /dev/sdb1Check for bad blocks during repair:
# Read-only bad block scan + filesystem checksudo e2fsck -c /dev/sdb1
# Read-write bad block scan (DESTRUCTIVE - erases data)sudo e2fsck -cc /dev/sdb1Surface Scan for Bad Blocks (badblocks)
Section titled “Surface Scan for Bad Blocks (badblocks)”Read-only test (safe):
# Non-destructive scan, show progresssudo badblocks -sv /dev/sdb
# Save bad block list to filesudo badblocks -sv -o badblocks.txt /dev/sdb
# Use bad block list during filesystem checksudo e2fsck -l badblocks.txt /dev/sdb1Write test (DESTRUCTIVE):
# WARNING: Erases ALL data on devicesudo badblocks -wsv /dev/sdb
# Non-destructive read-write test (very slow)sudo badblocks -nsv /dev/sdbResize Filesystem (resize2fs)
Section titled “Resize Filesystem (resize2fs)”Expand filesystem:
# After expanding partition with gparted# Unmount firstsudo umount /dev/sdb1
# Check filesystem before resizingsudo e2fsck -f /dev/sdb1
# Expand to fill partitionsudo resize2fs /dev/sdb1
# Remountsudo mount /dev/sdb1 /mntShrink filesystem:
# Unmount and checksudo umount /dev/sdb1sudo e2fsck -f /dev/sdb1
# Shrink to specific size (e.g., 50GB)sudo resize2fs /dev/sdb1 50G
# Now shrink partition with gparted to matchAdvanced Operations
Section titled “Advanced Operations”Recover Deleted Files (debugfs)
Section titled “Recover Deleted Files (debugfs)”Interactive recovery:
# Open filesystem in debugfssudo debugfs /dev/sdb1
# Inside debugfs:debugfs> lsdel # List recently deleted filesdebugfs> ls -d <inode> # Check if file recoverabledebugfs> dump <inode> /tmp/recovered_file # Recover filedebugfs> quitModify Filesystem Parameters (tune2fs)
Section titled “Modify Filesystem Parameters (tune2fs)”View current settings:
# Display all filesystem parameterssudo tune2fs -l /dev/sdb1 | lessOptimize for USB drives:
# Disable journaling (faster for flash drives)sudo tune2fs -O ^has_journal /dev/sdb1
# Reduce reserved space from 5% to 1%sudo tune2fs -m 1 /dev/sdb1Enable/disable features:
# Enable journalingsudo tune2fs -j /dev/sdb1
# Change to writeback mode (faster, less safe)sudo tune2fs -o journal_data_writeback /dev/sdb1
# Set filesystem labelsudo tune2fs -L "MyDrive" /dev/sdb1
# Set max mount count before forced checksudo tune2fs -c 30 /dev/sdb1
# Disable automatic time-based checkssudo tune2fs -i 0 /dev/sdb1Backup and Restore Metadata (e2image)
Section titled “Backup and Restore Metadata (e2image)”Create metadata backup:
# Backup filesystem metadata (not file contents)sudo e2image -r /dev/sdb1 - | gzip > sdb1_metadata.img.gz
# Raw backup including file contents (use for recovery)sudo e2image -ra /dev/sdb1 sdb1_backup.imgRestore metadata:
# Restore metadata from backupzcat sdb1_metadata.img.gz | sudo e2image -I /dev/sdb1 -Change File Attributes (chattr/lsattr)
Section titled “Change File Attributes (chattr/lsattr)”Make files immutable:
# Prevent deletion/modification (even by root)sudo chattr +i important_file.txt
# View attributeslsattr important_file.txt
# Remove immutable flagsudo chattr -i important_file.txtAppend-only files:
# Only allow appending (good for logs)sudo chattr +a logfile.txt