Essential Linux Disk Management Commands

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

CommandPurposeExample
dfDisk usagedf -h
duDirectory sizedu -sh /home
lsblkList block deviceslsblk -f
fdiskPartition toolfdisk -l
mkfsCreate filesystemmkfs.ext4 /dev/sda1
mountMount filesystemmount /dev/sda1 /mnt
umountUnmount filesystemumount /mnt
fsckCheck filesystemfsck -y /dev/sda1
smartctlDisk healthsmartctl -a /dev/sda
lsofOpen fileslsof -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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值