Linux file system commands: ext4, XFS, Btrfs and NTFS
Cheat sheet to create, check, resize, defragment and modify ext4, XFS, Btrfs and NTFS file systems on Linux.
Cheat sheet of common commands to manage ext4, XFS, Btrfs and NTFS file systems on Linux. It is based on the article File systems on Linux (LinuxTricks, in French). Replace /dev/XXX with the relevant device.
Overview
| Operation | ext4 | XFS | Btrfs |
|---|---|---|---|
| Create | mkfs.ext4 |
mkfs.xfs |
mkfs.btrfs |
| Information | tune2fs -l |
xfs_info |
btrfs filesystem show |
| Check / repair | e2fsck |
xfs_repair |
btrfs check --repair, btrfs scrub |
| Grow | resize2fs (online) |
xfs_growfs (online) |
btrfs filesystem resize (online) |
| Shrink | resize2fs (unmounted) |
not possible | btrfs filesystem resize (online) |
| Defragment | e4defrag |
xfs_fsr |
btrfs filesystem defragment |
| Label | tune2fs -L |
xfs_admin -L |
btrfs filesystem label |
| UUID | tune2fs -U |
xfs_admin -U |
btrfstune -U |
The -L (label) and -U (UUID) options of mkfs.* set them at creation time. A valid UUID can be generated with uuidgen. Changing the UUID requires an unmounted file system.
ext4
mkfs.ext4 /dev/XXX
tune2fs -l /dev/XXX
Check and repair
The file system must be unmounted.
e2fsck /dev/XXX
# or
fsck.ext4 /dev/XXX
Useful options: -y answers yes to every question, -f forces a check even if the file system is marked clean, -v enables verbose mode.
Grow and shrink
Growing is done online. Without a size, all available space is used.
resize2fs /dev/XXX 15G
Shrinking is done offline (unmounted), after a check.
e2fsck -f /dev/XXX
resize2fs /dev/XXX 10G
# Shrink to the minimum size
e2fsck -f /dev/XXX
resize2fs -M /dev/XXX
Defragment
Mostly useful on large volumes and spinning disks.
e4defrag -v /dev/XXX
Modify the file system
# Label
tune2fs -L NEWLABEL /dev/XXX
# UUID (unmounted)
tune2fs -U <uuid> /dev/XXX
# Blocks reserved for root: 5% by default, lowered here to 1%
tune2fs -m 1 /dev/XXX
On a large data volume the default 5% reservation is significant (about 50 GB on 1 TB). Setting 0% is not ideal; 1% is a compromise.
Periodic checks
tune2fs -l shows Maximum mount count (mounts before a check, -1 disables) and Check interval (maximum time in seconds between checks, 0 disables).
# One check every 50 mounts
tune2fs -c 50 /dev/XXX
# One check every 30 days
tune2fs -i 30d /dev/XXX
XFS
mkfs.xfs /dev/XXX
xfs_info /dev/XXX
Check and repair
The file system must be unmounted.
xfs_repair /dev/XXX
Grow
Growing is done online; the size is given in blocks (usually 4 KB, see xfs_info). Without a size, all available space is used. Example to reach about 15 GB:
xfs_growfs -D 3662109 /mount/point
Shrinking an XFS file system is not possible.
Defragment, label and UUID
xfs_fsr -v /dev/XXX
xfs_admin -L NEWLABEL /dev/XXX
xfs_admin -U <uuid> /dev/XXX
Btrfs
The commands below treat Btrfs as a plain file system (stable since kernel 4.14), without advanced features such as RAID, which were not yet considered stable.
mkfs.btrfs /dev/XXX
btrfs filesystem show /dev/XXX
To see used space, prefer:
btrfs filesystem df /mount/point
# or, more verbose
btrfs filesystem usage /mount/point
Check and repair
# Unmounted
btrfs check --repair /dev/XXX
# Online check (the file system cannot be unmounted during a scrub)
btrfs scrub start /dev/XXX
btrfs scrub status /dev/XXX
Grow and shrink
Both are done online. The size is in M or G; a + or - prefix adds or removes space; max uses all available space.
btrfs filesystem resize 15G /mount/point
btrfs filesystem resize +1G /mount/point
btrfs filesystem resize max /mount/point
Defragment and compress
btrfs filesystem defragment -rv /mount/point
# Defragment with lzo compression
btrfs filesystem defragment -rv -clzo /mount/point
For automatic compression, add compress=lzo to the mount options in /etc/fstab.
Label and UUID
btrfs filesystem label /mount/point LABEL
# Unmounted
btrfs filesystem label /dev/XXX LABEL
# UUID (unmounted)
btrfstune -U <uuid> /dev/XXX
Check the root file system
The root partition cannot be unmounted. The fsck field (last number of the line in /etc/fstab) sets the check order: 1 first, then 2.
Without systemd, create the /forcefsck file and reboot. It is removed automatically after the reboot. The result can be verified with tune2fs -l /dev/XXX | grep "Last checked".
touch /forcefsck
reboot
With systemd, the systemd-fsck@.service service is triggered by a kernel parameter: at boot, edit the GRUB line starting with linux and append:
fsck.mode=force
Unmount a busy file system
# umount /home
umount: /home: target is busy.
First identify the processes with fuser, then inspect them instead of killing them with kill -9.
fuser -m /home
ps -ef | grep <pid>
Stop the offending service cleanly, unmount, then run the check.
umount /home
fsck -f /dev/XXX
NTFS (FUSE)
FUSE (Filesystem in Userspace) lets an unprivileged user access a file system without modifying the kernel. Read/write access to NTFS requires the ntfs-3g package, often installed by default.
# Create (-Q: quick format, -L: label)
mkfs.ntfs -Q -L LABEL /dev/XXX
# Information
ntfsinfo -m /dev/XXX
# Check / repair (unmounted)
ntfsfix /dev/XXX
# Grow or shrink (size in bytes or with a unit)
ntfsresize --size 40G /dev/XXX
# Label
ntfslabel /dev/XXX LABEL
NTFS partitions cannot be defragmented from Linux.
Notes
- Back up before any repair or resize.
- When shrinking a logical volume or partition, shrink the file system first.