A Guide to Linux Disk Management

Managing disks in a Linux environment is crucial for maintaining system performance, storage organization, and data integrity. This guide covers essential Linux disk management tasks, providing examples for each operation.

1. Viewing Disk Information:

a. lsblk Command

Use lsblk to list information about block devices:

lsblk

b. fdisk Command

Display and manipulate disk partition tables:

sudo fdisk -l

2. Partitioning Disks:

a. Using fdisk or parted

Interactive tools for creating, deleting, and modifying partitions:

sudo fdisk /dev/sdX
# or
sudo parted /dev/sdX

3. Formatting Partitions:

a. mkfs Command

Format a partition with a specific file system (e.g., ext4):

sudo mkfs.ext4 /dev/sdXY

4. Mounting and Unmounting:

a. mount Command

Mount a file system to a specified mount point:

sudo mount /dev/sdXY /mnt/point

b. umount Command

Unmount a mounted file system:

sudo umount /mnt/point

5. Checking Disk Health:

a. smartctl Command

Check the SMART status of a drive:

sudo smartctl -a /dev/sdX

6. Monitoring Disk Usage:

a. df Command

Display information about disk space usage:

df -h

b. du Command

Show disk usage of files and directories:

du -h /path/to/directory

7. Managing Swap Space:

a. swapon and swapoff Commands

Activate or deactivate swap space:

sudo swapon /path/to/swapfile
# or
sudo swapoff /path/to/swapfile

8. Checking File System:

a. fsck Command

Check and repair a Linux file system:

sudo fsck /dev/sdXY

9. Disk Quotas:

a. Quota Management Commands

Manage disk quotas for users:

sudo quotacheck -avug
sudo quotaon -avug
sudo edquota username

10. USB and External Drives:

a. lsusb Command

List USB devices connected:

lsusb

b. Mounting and Unmounting (for external drives)

Mount and unmount external drives similar to internal drives:

sudo mount /dev/sdXY /mnt/point
sudo umount /mnt/point

Important Notes:

  • Always double-check the device path (e.g., /dev/sdXY) to avoid accidental data loss.
  • Exercise caution when manipulating partitions or formatting drives, as these actions can result in data loss.
  • Regularly monitor disk health and perform backups to prevent data loss.

Commands and tools may vary depending on your Linux distribution. Refer to your distribution’s documentation for specific details.