Monitoring disk space is critical for any Linux system administrator. When a disk starts getting full, it can cause the system to slow down, programs to crash unexpectedly, and data corruption. That‘s why it‘s important to keep an eye on disk usage.
In this comprehensive guide, I‘ll explain several methods for checking disk space on an Ubuntu system, both from the command line and graphical user interfaces. As an experienced Linux system administrator, I‘ll provide in-depth details on interpreting the output to accurately gauge your disk usage.
Checking Disk Space from the Terminal
The terminal provides several powerful commands for monitoring disk usage in Ubuntu. These give you granular details and allow you to script or schedule checks. Here are the primary command line tools:
df
The df
(disk filesystem) command shows overall disk usage for mounted filesystems. Its simplest form gives totals in 1k blocks by default:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 3971436 0 3971436 0% /dev
tmpfs 802916 8580 794336 2% /run
/dev/sda1 30716928 887352 28505564 3% /
tmpfs 4014580 0 4014580 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 4014580 0 4014580 0% /sys/fs/cgroup
/dev/sda15 1052248 587808 430248 60% /boot
tmpfs 802912 0 802912 0% /run/user/121
tmpfs 802912 4 802908 1% /run/user/1000
This shows each filesystem, total blocks, used blocks, available blocks, percentage used and where it‘s mounted.
To get human-readable sizes, use -h
. This displays in more understandable formats like megabytes and gigabytes instead of blocks:
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.8G 0 3.8G 0% /dev
tmpfs 786M 8.4M 777M 2% /run
/dev/sda1 29G 13G 15G 47% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda15 1.0G 573M 383M 60% /boot
tmpfs 786M 0 786M 0% /run/user/121
tmpfs 786M 4.0K 786M 1% /run/user/1000
Now you can clearly see that my root (/) filesystem is 29GB total, with 13GB used and 15GB still free. The -h
option is very useful for quick checks on disk space.
du
While df
shows overall filesystem disk usage, du
(disk usage) checks disk space used by individual directories and files. Its simplest form gives totals in 1k blocks like df
:
$ du
8272 ./logs
12964 ./config
16040 .
This shows the total space used by the logs folder, config folder, and current directory (.).
Adding -h
shows the sizes in human-readable formats again:
$ du -h
8.1M ./logs
12M ./config
16M .
Now you can see the logs folder is using 8.1MB, config is 12MB, and the current folder is 16MB.
Adding -s
gives a summary total instead of listing every subdirectory:
$ du -sh
16M .
du
is very useful for finding which folders are using the most disk space. For example, to check usage grouped by largest first:
$ du -hk * | sort -h
This profiles disk usage by folder, shows human-readable formats in KB, and sorts the output largest to smallest.
ncdu
If you want an interactive terminal-based disk usage analyzer, ncdu
is a great option. It scans disks and directories, then lets you navigate folders and sort by size in its interface:
Install ncdu
by:
sudo apt install ncdu
Then run it by typing ncdu
in any directory to scan that location. The interface is completely text-based and allows drilling down folders and sorting to find disk usage hotspots.
Checking Disk Space Graphically
If you prefer a graphical look at your disk usage, Ubuntu provides several good options:
Disk Usage Analyzer
The standard Disk Usage Analyzer (also called Baobab) provides a graphical breakdown of disk usage.
Open it from the desktop Start menu or by running baobab
in the terminal. Then click your filesystem to scan. It provides a color coded treemap showing large files and folders. You can drill down and search for specific items.
Disks Utility
For a quick overview, the built-in Disks utility (also called gnome-disks) shows free space and basic details:
Search for "Disks" in the Start menu to open. It scans all disks and shows free space vs used space. Click each filesystem to see further details and analyze usage. This is the easiest graphical option if you just want a quick dashboard read-out of disk usage.
Scheduling Disk Space Checks
Instead of manually running disk checks, you may want to automatically get warnings if a disk starts getting full. This helps prevent out of space errors before they happen.
Here are a couple ways to schedule disk space monitoring:
cron Job
Add a cron job that runs df -h
on a schedule and emails you the output.
First create a script called check_disk.sh
:
#!/bin/bash
df -h / >> /tmp/disk_report
Make it executable:
chmod +x check_disk.sh
Then create a cron job to run it daily:
0 0 * * * /path/to/check_disk.sh && mail -s "Disk Space Report" you@domain.com < /tmp/disk_report
This will now check disk space every day at midnight and email you the results so you get a daily report. Adjust the schedule as needed.
systemd Timer
For more complex scheduling, you can create systemd timer unit files. These allow setting calendars, intervals, random delays and other options for running tasks.
For example, create diskcheck.timer
:
[Unit]
Description=Runs disk check daily
[Timer]
OnBootSec=5min
OnUnitActiveSec=1d
Unit=diskcheck.service
[Install]
WantedBy=timers.target
And diskcheck.service
:
[Unit]
Description=Check disk space
[Service]
Type=oneshot
ExecStart=/path/to/check_disk.sh
These will run check_disk.sh
5 minutes after boot, then every 1 day after that. See the systemd documentation for other timing options.
Conclusion
There are many handy options for checking disk usage on Ubuntu Linux systems. Both the CLI tools like df
, du
and ncdu
as well graphical apps like Baobab provide detailed breakdowns. And you can schedule automatic checks to get alerts about low disk space before it causes issues.
As you can see, Linux offers very robust tools for monitoring available disk space. Keeping an eye on your filesystem usage is easy.
Let me know in the comments if you have any other favorite disk space commands or software on Ubuntu!