Introduction
Disk and storage management is crucial for maintaining system health, optimizing performance, and preventing data loss. From monitoring disk usage to managing partitions and filesystems, these commands help you effectively manage storage resources. This guide covers 10 essential disk management commands.
1. df - Disk Space Usage
Display disk space usage for filesystems.
# Show disk usage for all filesystems
df
# Show in human-readable format
df -h
# Show specific filesystem
df -h /home
# Show inode usage
df -i
# Show filesystem type
df -T
# Show total usage
df -h --total
2. du - Disk Usage by Directory
Estimate file and directory space usage.
# Show directory sizes
du -sh /home/*
# Show all files and directories
du -ah /var/log
# Show only total for directory
du -sh /var/log
# Show top 10 largest directories
du -h --max-depth=1 / | sort -hr | head -10
# Show inode usage
du -in /home
3. lsblk - List Block Devices
Display information about block devices.
# List all block devices
lsblk
# Show filesystem type
lsblk -f
# Show all devices including empty
lsblk -a
# Show device size and mount point
lsblk -o NAME,SIZE,MOUNTPOINT
# Show UUID and LABEL
lsblk -o NAME,UUID,LABEL,SIZE
# Show filesystem information
lsblk -fs
4. fdisk - Partition Table Manipulator
View and modify disk partitions.
# List all partitions
sudo fdisk -l
# List specific disk
sudo fdisk -l /dev/sda
# Open disk for editing
sudo fdisk /dev/sda
# Inside fdisk interactive mode:
# p - Print partition table
# n - New partition
# d - Delete partition
# w - Write changes
# q - Quit without saving
WARNING: fdisk modifies disk partitions. Use with caution!
5. mkfs - Create Filesystem
Format a partition with a filesystem.
# Create ext4 filesystem
sudo mkfs.ext4 /dev/sdb1
# Create ext3 filesystem
sudo mkfs.ext3 /dev/sdb1
# Create XFS filesystem
sudo mkfs.xfs /dev/sdb1
# Create with label
sudo mkfs.ext4 -L data /dev/sdb1
# Check for bad blocks
sudo mkfs.ext4 -c /dev/sdb1
6. mount - Mount Filesystems
Mount filesystems to directory locations.
# Mount a device
sudo mount /dev/sdb1 /mnt/data
# Mount with filesystem type
sudo mount -t ext4 /dev/sdb1 /mnt/data
# Mount read-only
sudo mount -o ro /dev/sdb1 /mnt/data
# Remount with different options
sudo mount -o remount,rw /mnt/data
# Mount ISO image
sudo mount -o loop image.iso /mnt/iso
# Auto-mount from /etc/fstab
sudo mount -a
7. umount - Unmount Filesystems
Safely unmount filesystems.
# Unmount by mount point
sudo umount /mnt/data
# Unmount by device
sudo umount /dev/sdb1
# Lazy unmount (detach immediately)
sudo umount -l /mnt/data
# Force unmount
sudo umount -f /mnt/data
# Verbose unmount
sudo umount -v /mnt/data
8. fsck - Filesystem Check
Check and repair filesystem errors.
# Check filesystem (auto-fix)
sudo fsck -y /dev/sdb1
# Check ext4 filesystem
sudo fsck.ext4 /dev/sdb1
# Check with interactive repair
sudo fsck -r /dev/sdb1
# Check all filesystems in fstab
sudo fsck -A
# Force check even if clean
sudo fsck -f /dev/sdb1
WARNING: Never run fsck on mounted filesystems!
9. smartctl - SMART Disk Health
Monitor disk health using SMART data.
# Check disk health
sudo smartctl -H /dev/sda
# Show all SMART info
sudo smartctl -a /dev/sda
# Run short self-test
sudo smartctl -t short /dev/sda
# Run long self-test
sudo smartctl -t long /dev/sda
# View test results
sudo smartctl -l selftest /dev/sda
# Enable SMART
sudo smartctl -s on /dev/sda
10. lsof - List Open Files
List open files and processes using them.
# Show all open files
lsof
# Show files in specific directory
lsof +D /var/log
# Show files by user
lsof -u john
# Show network connections
lsof -i
# Show processes using port
lsof -i :80
# Show files by process ID
lsof -p 1234
# Show deleted files still open
lsof | grep deleted
Quick Reference Table
| Command | Purpose | Example |
|---|---|---|
| df | Disk usage | df -h |
| du | Directory size | du -sh /home |
| lsblk | List block devices | lsblk -f |
| fdisk | Partition tool | fdisk -l |
| mkfs | Create filesystem | mkfs.ext4 /dev/sda1 |
| mount | Mount filesystem | mount /dev/sda1 /mnt |
| umount | Unmount filesystem | umount /mnt |
| fsck | Check filesystem | fsck -y /dev/sda1 |
| smartctl | Disk health | smartctl -a /dev/sda |
| lsof | Open files | lsof -i :80 |
Conclusion
These 10 disk management commands enable you to monitor disk space usage, manage partitions and filesystems, mount and unmount storage devices, check and repair filesystem errors, monitor disk health with SMART, and identify open files and processes.
Related Articles:

755

被折叠的 条评论
为什么被折叠?



