Common Linux Disk Management Commands

1. Common Disk Management Commands

Command Description Core Purpose
df Displays disk space usage of file systems View overall disk usage and remaining space
du Counts disk usage of files/directories Analyze storage usage of specific directories or files
fdisk Disk partition management tool Create, delete, and adjust disk partitions
mkfs Formats a disk partition to a specified file system Initialize a partition to a usable file system
fsck Checks and repairs file systems Maintain file system consistency
mount/umount Mount/unmount disk devices Manage access paths to storage devices

2. Command Details and Examples

1. df: View Disk Space Usage

Syntax:
df [options] [directory or file]
Common Options:
  • -h: Display in human-readable format (e.g., GB/MB)

  • -T: Show file system type

  • -i: View inode usage

  • -a: Display all file systems (including virtual file systems)

Examples:
# Display disk usage of all file systems (human-readable format)
df -h

# View space usage of the partition containing /etc
df -h /etc

# Display file system type and inode information
df -aTi

2. du: Count Disk Usage of Files/Directories

Syntax:
du [options] [file or directory]
Common Options:
  • -h: Display in human-readable format

  • -s: Show only total size (do not recurse into subdirectories)

  • -a: Count all files (including hidden files)

  • --max-depth=N: Limit the depth of directory statistics

Examples:
# Count total size of the current directory (human-readable format)
du -sh .

# List sizes of subdirectories under /var (1 level deep)
du -h --max-depth=1 /var

# Display disk usage of all files (including hidden files)
du -ah ~/Documents

3. fdisk: Disk Partition Management

Syntax:
fdisk [options] [disk device]
Common Operations:
  • fdisk -l: List all disk partition information

  • fdisk /dev/sdX: Enter interactive mode to manage the specified disk

Interactive Mode Commands:
Command Function
n Create a new partition
d Delete a partition
p Print the current partition table
w Save and exit
q Quit without saving changes
Examples:
# View all disk partition information
fdisk -l

# Manage disk /dev/sdb
fdisk /dev/sdb
# Enter `n` → Set partition type → Enter `w` to save

4. mkfs: Format Disk Partition

Syntax:
mkfs -t [file system type] [partition device]
Supported File Systems:
  • ext4, ext3, vfat, xfs, etc.

Examples:
# Format /dev/sdb1 as ext4
mkfs -t ext4 /dev/sdb1

# Format as FAT32 file system
mkfs -t vfat /dev/sdb2

5. fsck: File System Check and Repair

Syntax:
fsck [options] [partition device]
Common Options:
  • -y: Automatically fix errors

  • -f: Force check (even if the file system status is normal)

  • -C: Display check progress bar

Examples:
# Force check and repair /dev/sdb1 (ext4 file system)
fsck -f -y -t ext4 /dev/sdb1

6. mount/umount: Mount and Unmount Disks

(1) Mount Syntax:
mount [-t file system type] [-o mount options] [device name/UUID/Label] [mount point]
Common Options
Option Description
-t Specify file system type (e.g., ext4, ntfs, vfat, xfs, etc.)
-o Specify mount options (multiple options separated by commas, see below for -o sub-option table)
-L Mount device by label (e.g., -L DATA_DRIVE)
-n Do not update /etc/mtab file when mounting (suitable for read-only environments)
-r Mount in read-only mode (equivalent to -o ro)
-a Mount all devices defined in /etc/fstab (usually used with -t)
Usage Examples
# Mount /dev/sdb1 to /mnt/data (auto-detect file system)
mount /dev/sdb1 /mnt/data

# Specify file system type as NTFS (requires ntfs-3g installed)
mount -t ntfs-3g /dev/sdc1 /mnt/ntfs

# Mount in read-only mode and disable execute permissions
mount -o ro,noexec /dev/sdd1 /mnt/backup

# Mount by label (Label is MY_DISK)
mount -L MY_DISK /mnt/disk

# Remount as read-write mode (originally read-only)
mount -o remount,rw /dev/sdb1
(2) Unmount Syntax:
umount [options] [device name/mount point]
Common Options
Option Description
-f Force unmount (suitable for busy file systems or when unable to unmount normally)
-l Lazy unmount (immediately disconnect the file system, clean up resources in the background)
-n Do not update /etc/mtab file when unmounting
-v Display detailed operation information
-r If unmount fails, try to remount in read-only mode
Usage Examples
# Normal unmount of mount point
umount /mnt/data

# Force unmount device (suitable for processes occupying the device)
umount -f /dev/sdb1

# Lazy unmount (immediately release mount point, clean up resources in the background)
umount -l /mnt/backup

# Display detailed information during unmounting
umount -v /dev/sdc1

3. Configuration Files and Precautions

1. /etc/fstab: Automatic Mount Configuration

  • Format:

    [device/UUID] [mount point] [file system] [mount options] [backup flag] [self-check order]
  • Example:

    UUID=abcd1234 /mnt/data ext4 defaults 0 0

2. Precautions

  • Operation Risks: Commands like fdisk, mkfs, etc., will directly modify disk data; back up important data before operating.

  • Unmount Requirements: Ensure no processes are accessing the mount point before unmounting (can check with lsof [mount point]).

  • File System Compatibility: Different systems support different file systems (e.g., Windows does not directly support ext4).

4. Quick Command Reference Table

Scenario Command Example
View disk space df -h
Count directory size du -sh /path/to/dir
Create a new partition fdisk /dev/sdXnw
Format partition as ext4 mkfs -t ext4 /dev/sdX1
Force check file system fsck -f /dev/sdX1
Automatically mount at boot Edit /etc/fstab
Unmount busy device umount -l [mount point] (lazy unmount)

Leave a Comment