Loading...

Linux Disk Management: df, du, fdisk, parted, mount, and LVM Basics

Back to blog
LinuxDisk ManagementDevOpsSystem Administration

Linux Disk Management: df, du, fdisk, parted, mount, and LVM Basics

Master Linux disk management including disk usage with df and du, partitioning with fdisk and parted, mounting filesystems, and LVM for flexible storage.

9 min read

Linux Disk Management: df, du, fdisk, parted, mount, and LVM Basics

Disk management is crucial for system administrators. Understanding how to monitor disk usage, partition disks, and manage storage ensures system stability and prevents running out of space.

Monitoring Disk Usage

df - Disk Free

Show filesystem disk space usage.

# View disk usage
df
 
# Human-readable format
df -h
# Filesystem  Size  Used Avail Use% Mounted on
# /dev/sda1   20G   8.5G  10G  45%  /
# /dev/sda2   40G   25G   15G  63%  /home
 
# Show only specific filesystem
df /home
 
# Show inode usage
df -i
# Shows inode utilization
 
# Type of filesystem
df -T
# Filesystem Type
 
# All filesystems (including virtual)
df -a
 
# Exclude filesystem types
df -x tmpfs
df -x devtmpfs
 
# Only local filesystems
df -l
 
# Sort by usage
df -h | sort -k3 -h
 
# Show specific columns
df -h -B G  # Size in GB
df -h -B M  # Size in MB
 
# Follow symlinks
df -L
 
# Show total
df -h | tail -1

du - Disk Usage

Show directory disk usage.

# Show directory size
du /home/john
 
# Human-readable format
du -h /home/john
# 4.0K    .bash_history
# 8.0K    .config
# 12K     .local
# 100K    Documents
# 500K    total
 
# Show only total
du -sh /home/john
# 500K    /home/john
 
# Show per-directory
du -sh /home/*/
# Shows total for each user
 
# Limit depth
du -h --max-depth=1 /home
du -h -d 1 /home
 
# Show only directories larger than size
du -h /home | grep '^[0-9]*M'
du -h --threshold=100M /home
 
# Sorted output
du -sh /home/* | sort -h
du -sh /home/* | sort -rh  # Largest first
 
# Count files
du --inodes /home
 
# Exclude directories
du -h --exclude=.git /home/project
 
# Display all files
du -a /home
 
# Find largest files
du -ah /home | sort -h | tail -20
 
# Find largest directories
du -h /home | sort -rh | head -10
 
# Update rate
du /home &  # Background
# Updates as running
 
# Compare before and after
du -s /home > before.txt
# make changes
du -s /home > after.txt
diff before.txt after.txt

ncdu - Visual Disk Usage

Interactive disk usage analysis.

# Install ncdu
sudo apt install ncdu
 
# Scan directory
ncdu /home
 
# Scan and save to file
ncdu -o ncdu.db /home
 
# Load saved scan
ncdu -f ncdu.db
 
# Exclude directories
ncdu --exclude .git /home
 
# Show aggregate blocks
ncdu -r /home
 
# Scan filesystem
sudo ncdu /
 
# Interactive navigation
# n: Sort by name
# s: Sort by size
# d: Delete selected
# q: Quit

Partitioning Disks

lsblk - List Block Devices

Show block device information.

# Show all block devices
lsblk
# NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
# sda    8:0     0 100G  0 disk
# ├─sda1 8:1     0  20G  0 part /
# ├─sda2 8:2     0  50G  0 part /home
# └─sda3 8:3     0  30G  0 part [SWAP]
# sdb    8:16    0 200G  0 disk
 
# Show filesystem information
lsblk -f
# NAME FSTYPE LABEL UUID
 
# Show sizes in bytes
lsblk -b
 
# Show all information
lsblk -a
 
# Tree format
lsblk -t
 
# No header
lsblk -n
 
# Custom columns
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT

blkid - Block Device ID

Show block device identification.

# Show all block devices
sudo blkid
 
# Show specific device
sudo blkid /dev/sda1
 
# Show by UUID
sudo blkid -U uuid
 
# Show by label
sudo blkid -L label
 
# Show as format
sudo blkid -o full
 
# Output as shell variables
sudo blkid -o export /dev/sda1

fdisk - Partition Editor (MBR)

Manage MBR partition tables.

# List partitions
sudo fdisk -l
 
# List specific disk
sudo fdisk -l /dev/sda
 
# Show partition table
sudo fdisk /dev/sda
# Command prompt appears
 
# Common fdisk commands:
# m: Show menu
# n: New partition
# d: Delete partition
# p: Print table
# t: Change type
# w: Write changes
# q: Quit without saving
 
# Example: Create partition
sudo fdisk /dev/sdb
# Command (m for help): n
# Partition type: p (primary)
# Partition number: 1
# First sector: (default)
# Last sector: +10G
# Command (m for help): w
 
# Remove partition
sudo fdisk /dev/sdb
# Command: d
# Partition number: 1
# Command: w

parted - Partition Editor (GPT)

Advanced partition management (supports GPT).

# Show partition table
sudo parted /dev/sda print
 
# Interactive mode
sudo parted /dev/sda
 
# List all disks
sudo parted -l
 
# Create new partition table
sudo parted /dev/sdb mklabel gpt
 
# Create partition
sudo parted /dev/sdb mkpart primary ext4 1MB 10GB
 
# Delete partition
sudo parted /dev/sdb rm 1
 
# Resize partition
sudo parted /dev/sda resizepart 1 50GB
 
# Show details
sudo parted /dev/sda unit GB print
 
# Non-interactive usage
sudo parted /dev/sdb -- mkpart primary ext4 -0 -1

Creating Filesystems

mkfs - Make Filesystem

# Create ext4 filesystem
sudo mkfs -t ext4 /dev/sdb1
 
# Create ext4 with label
sudo mkfs.ext4 -L "MyDisk" /dev/sdb1
 
# Create ext3
sudo mkfs -t ext3 /dev/sdb1
 
# Create FAT32
sudo mkfs.vfat /dev/sdb1
 
# Create NTFS
sudo mkfs.ntfs /dev/sdb1
 
# Create XFS
sudo mkfs.xfs /dev/sdb1
 
# Check before formatting
sudo fdisk -l /dev/sdb
 
# Block size (advanced)
sudo mkfs.ext4 -b 4096 /dev/sdb1
 
# Inode ratio
sudo mkfs.ext4 -i 16384 /dev/sdb1
 
# Clear filesystem
sudo mkfs.ext4 -F /dev/sdb1  # Force

Mounting Filesystems

mount - Attach Filesystem

# View mounted filesystems
mount
 
# Mount specific device
sudo mount /dev/sdb1 /mnt/external
 
# Mount with options
sudo mount -o ro /dev/sdb1 /mnt/external  # Read-only
sudo mount -o noexec /dev/sdb1 /mnt      # No execution
 
# Mount specific filesystem type
sudo mount -t ntfs /dev/sdb1 /mnt/
 
# Mount with permissions
sudo mount -o umask=0022 /dev/sdb1 /mnt
 
# Create mount point
sudo mkdir -p /mnt/external
 
# Bind mount (directory to directory)
sudo mount --bind /home/john/backup /mnt/backup
 
# Remount with different options
sudo mount -o remount,noexec /mnt
 
# List available filesystems
cat /etc/filesystems
 
# Show filesystem tree
findmnt
 
# Show specific mount
findmnt /home

Unmount Filesystem

# Unmount device
sudo umount /dev/sdb1
 
# Unmount by mount point
sudo umount /mnt/external
 
# Force unmount (lazy)
sudo umount -l /mnt/external
 
# Unmount all
sudo umount -a
 
# Check if in use
lsof /mnt/external
# Find processes using mount
 
# Wait for access to complete
fuser -u /mnt/external

/etc/fstab - Automatic Mounting

# Edit fstab for persistent mounts
sudo nano /etc/fstab
 
# fstab format:
# device mount_point type options dump fsck_order
 
# Example entries:
# /dev/sda1 /           ext4 defaults         0 1
# /dev/sda2 /boot       ext4 defaults         0 2
# /dev/sda3 /home       ext4 defaults         0 2
# /dev/sdb1 /mnt/external ext4 defaults,noexec 0 0
 
# UUID mount (recommended)
# UUID=1234-5678 /mnt/external ext4 defaults 0 0
 
# LABEL mount
# LABEL=MyDisk /mnt/external ext4 defaults 0 0
 
# Verify fstab syntax
sudo mount -a
 
# Find UUID
sudo blkid
 
# Create mount point if needed
sudo mkdir -p /mnt/external
 
# Common mount options:
# defaults: rw, suid, dev, exec, auto, nouser, async
# noexec: No program execution
# nosuid: No setuid
# nodev: No device access
# ro: Read-only
# rw: Read-write

LVM - Logical Volume Management

Advanced storage management for flexibility.

Physical Volumes

# Create physical volume
sudo pvcreate /dev/sdb1
sudo pvcreate /dev/sdc1
 
# View physical volumes
sudo pvdisplay
 
# Show PV in table format
sudo pvs
 
# Check physical volume
sudo pvcreate /dev/sdb1
 
# Remove physical volume
sudo pvremove /dev/sdb1

Volume Groups

# Create volume group
sudo vgcreate myvg /dev/sdb1 /dev/sdc1
 
# View volume groups
sudo vgdisplay
 
# List in table format
sudo vgs
 
# Extend volume group
sudo vgextend myvg /dev/sdd1
 
# Reduce volume group
sudo vgreduce myvg /dev/sdd1
 
# Remove volume group
sudo vgremove myvg

Logical Volumes

# Create logical volume
sudo lvcreate -L 10G -n mylv myvg
 
# Create with percentage
sudo lvcreate -l 50%VG -n mylv myvg
 
# View logical volumes
sudo lvdisplay
 
# List in table format
sudo lvs
 
# Extend logical volume
sudo lvextend -L +5G /dev/myvg/mylv
 
# Extend to maximum
sudo lvextend -l +100%FREE /dev/myvg/mylv
 
# Reduce logical volume
sudo lvreduce -L 5G /dev/myvg/mylv
 
# Remove logical volume
sudo lvremove /dev/myvg/mylv
 
# Resize filesystem after extending
sudo resize2fs /dev/myvg/mylv

Practical Disk Management Tasks

Find Large Files and Directories

# Largest directories
du -sh /home/* | sort -rh | head -10
 
# Largest files
find / -type f -exec du -h {} + | sort -rh | head -20
 
# Find duplicate files
find /home -type f -exec md5sum {} \; | sort | uniq -d -w 32

Clean Up Disk Space

# Remove old logs
find /var/log -name "*.log" -mtime +30 -delete
 
# Clear apt cache
sudo apt clean
sudo apt autoclean
 
# Remove old backups
find /backup -type f -mtime +90 -delete
 
# Clear temp files
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*
 
# Find and remove empty directories
find /home -type d -empty -delete

Monitor Disk Usage

# Create disk usage report
date > disk_report.txt
df -h >> disk_report.txt
du -sh /home/* >> disk_report.txt
 
# Watch disk usage
watch -n 5 'df -h && echo && du -sh /home/*'
 
# Alert if usage high
if [ $(df / | awk 'NR==2 {print $5}' | cut -d% -f1) -gt 90 ]; then
  echo "Disk space critical!"
fi

Best Practices

  1. Monitor Regularly - Check disk usage frequently
  2. Plan Partitions - Allocate space appropriately
  3. Use Persistent Mounts - Configure /etc/fstab
  4. Label Partitions - Use meaningful labels
  5. Backup Before Changes - Always backup data first
  6. Use UUID - More reliable than device names
  7. Consider LVM - For flexible storage
  8. Regular Cleanup - Remove old files and logs
  9. Monitor Inodes - Not just disk space
  10. Test Mounts - Verify before production use

Summary

Disk management is critical for system health:

  • df monitors filesystem usage
  • du analyzes directory usage
  • fdisk/parted manage partitions
  • mkfs creates filesystems
  • mount/umount manage access
  • LVM provides flexible storage
  • Proper management prevents data loss and system issues

Master disk management for reliable Linux systems.