Skip to main content

Managing Disk Space

1. Viewing Disk Usage

Command Description Notes
df -h Display disk space usage in human-readable format Shows filesystems, total size, used space, available space, and usage percentage
du -sh * Display disk usage of all items in current directory Summarizes space used by each file/directory, sorted by size
du -h --max-depth=1 /path Display disk usage up to one level deep in a specific directory Useful for analyzing large directories without going too deep

Example: Check disk usage of root directory

df -h /

Example: Find largest directories in home folder

du -sh ~/.* 2>/dev/null | sort -hr

2. Checking Directory Size

Command Description Notes
du -sh * Display disk usage of all directories in current folder Use -h for human-readable format
du -sh /path/to/dir Display disk usage of a specific directory Use -s to summarize total size
du -h --max-depth=1 /path/to/dir Display disk usage up to one level deep Useful for exploring directory structure

Example: Check directory size

$ du -sh /home/user/Documents
1.2G    /home/user/Documents

Example: Check all subdirectories size

$ du -sh /*
4.5G    /home
2.1G    /var
100M    /etc

3. Analyzing Disk Usage

Example: Check disk usage of current directory

du -sh .
Command Description Notes
du -sh /path Display disk usage of a specific path in human-readable format Use -h for readable units (KB, MB, GB)
du -h --max-depth=1 / Display disk usage of root directory with max depth of 1 level Useful for identifying large directories
du -h --threshold=1G / Display disk usage of directories larger than 1GB Useful for filtering large files/directories
Note: The du command can be slow on large filesystems. Consider using ncdu for interactive disk usage analysis.

Example: Using ncdu for interactive disk usage analysis

ncdu /home

Common du options

  • -h: Human-readable output (e.g., 1K, 234M)
  • -s: Summarize total usage for each directory
  • --max-depth=N: Limit recursion depth to N levels
  • --threshold=SIZE: Only show entries larger than SIZE

4. Finding Large Files

Example: Find files larger than 100MB

find / -type f -size +100M -exec ls -lh {} \; | awk '{print \$5 " " \$9}'
Command Description Notes
find / -type f -size +100M Searches for files larger than 100MB Use -size +100M to find files larger than 100MB
find / -type f -size -100M Searches for files smaller than 100MB Use -size -100M to find files smaller than 100MB
find / -type f -size 100M Searches for files exactly 100MB Use -size 100M to find files exactly 100MB
Note: The find command is powerful and can be slow on large filesystems. Consider limiting the search scope to specific directories.

Example: Find files larger than 1GB

find /home -type f -size +1G | head -n 5

Example: Find files larger than 1GB and sort by size

find /home -type f -size +1G -exec du -h {} \; | sort -hr

5. Checking Inode Usage

Command Description Notes
df -i Display inode usage for all mounted filesystems Shows inodes used, available, and percentage used
df -i /path/to/directory Display inode usage for a specific directory's filesystem Useful to check if a particular mount is running out of inodes
find /path -inum 12345 Find files with a specific inode number Can be used to locate files that are consuming inodes

Example: Check inode usage on root filesystem

df -i /
Filesystem      Inodes IUsed   IFree IUse% Mounted on
/dev/sda1     1000000 500000 500000   50% /
Note: Inodes are not the same as disk space — a filesystem can be full of data but have plenty of inodes, or it can run out of inodes even if there's plenty of disk space.

6. Viewing Block Devices

Device Type Size Mount Point
/dev/sda Hard Disk Drive 500GB /
/dev/sdb Solid State Drive 1TB /home
/dev/sdc USB Drive 32GB /mnt/usb

Example: Using lsblk to view block devices

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  465.8G  0 disk /
sdb      8:16   0    1T  0 disk /home
sdc      8:32   1   32G  0 disk /mnt/usb

Example: Using fdisk to view partition layout

$ fdisk -l /dev/sda
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt

Device     Start        End    Sectors   Size Type
/dev/sda1  2048   976773167   976771120 465.8G Linux filesystem

7. Displaying Partition Table

Command Description Notes
lsblk Lists block devices including partitions and their sizes Shows hierarchy of storage devices
fdisk -l Lists all disk partitions and their details Requires root privileges for full detail
parted -l Lists partition tables using parted utility More modern alternative to fdisk
df -h Shows disk space usage in human-readable format Displays filesystems and used/free space

Example: Using lsblk to display partition table

lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  100M  0 part /boot
└─sda2   8:2    0 49.9G  0 part /
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0   20G  0 part /mnt/data

8. Mounting File Systems

Command Description Example
mount Mounts a filesystem to a mount point sudo mount /dev/sdb1 /mnt/data
umount Unmounts a mounted filesystem sudo umount /mnt/data
mount -a Mounts all filesystems defined in /etc/fstab sudo mount -a

Example: Mounting a USB Drive

lsblk
sudo mkdir /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
Note: Ensure the device is not in use before mounting.

Example: Auto-mounting with /etc/fstab

/dev/sdb1 /mnt/data ext4 defaults 0 1

9. Unmounting File Systems

Command Description Usage
umount Unmounts a mounted filesystem umount /dev/sdb1
umount -l Lazily unmounts a filesystem (detaches it even if busy) umount -l /mnt/data
umount -f Forcefully unmounts a filesystem (use with caution) umount -f /media/usb

Example: Unmounting a USB Drive

# Check mounted filesystems
df -h

# Unmount the USB drive
umount /dev/sdb1
Note: Ensure no processes are using the filesystem before unmounting to avoid data corruption.
Warning: Using umount -f can lead to data loss if files are actively being written.

10. Viewing Mounted File Systems

Command Description Example Output
df -h Shows disk space usage in human-readable format
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   15G  4.0G  75% /
tmpfs           1.9G  236K  1.9G   1% /dev/shm
mount Lists all currently mounted filesystems
/dev/sda1 on / type ext4 (rw,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)
lsblk Lists block devices including partitions and mounted filesystems
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
├─sda1   8:1    0   20G  0 part /
└─sda2   8:2    0    2G  0 part [SWAP]

Example: Check disk usage for a specific directory

du -sh /var/log
Note: The df command shows filesystem usage, while du shows directory sizes.

11. Configuring Automatic Mounts

Option Values Description
/etc/fstab UUID=12345678-1234-1234-1234-123456789012 /home ext4 defaults 0 2 Filesystem table for automatic mounting at boot
mount -a mount -a Mount all filesystems listed in /etc/fstab
noauto noauto Prevents automatic mounting at boot
defaults defaults Default mount options (rw, suid, dev, exec, auto, nouser, async)
ro ro Mount filesystem read-only
rw rw Mount filesystem read-write

Example: Adding a New Mount Point

# Add to /etc/fstab
UUID=12345678-1234-1234-1234-123456789012 /home ext4 defaults 0 2

# Apply changes
mount -a
Note: Ensure the filesystem is properly formatted before adding to /etc/fstab.
Warning: Incorrect /etc/fstab entries can prevent system boot.

12. Checking File System

Command Description Example
df -h Display disk space usage in human-readable format df -h
du -sh * Summarize disk usage of directories in current folder du -sh *
lsblk List block devices with size and mount points lsblk
fdisk -l List all disk partitions and sizes fdisk -l

Example: Check disk usage of root directory

df -h /

Was this article helpful?