How to Check Disk Space on Linux
Checking disk space on Linux is something you’ll want to know how to do, whether you’re managing a personal computer or a server. Knowing how much space you have left helps you avoid running out of storage, which can cause system slowdowns or failures. In this article, I’ll guide you through easy and practical ways to check disk space on Linux, using commands and tools that work on most distributions.
You don’t need to be a Linux expert to follow along. I’ll explain everything in simple terms and show you how to get detailed information about your disk usage. By the end, you’ll feel confident managing your storage and keeping your system running smoothly.
Understanding Disk Space on Linux
Disk space refers to the amount of storage available on your hard drive or SSD. On Linux, your storage is divided into partitions or mounted file systems, each with its own capacity and usage. Checking disk space means seeing how much of that storage is used and how much is free.
Linux treats everything as a file, including disks and partitions. This means you can use commands to get detailed information about each mounted file system. Knowing this helps you manage your files better and avoid problems caused by full disks.
Why Checking Disk Space Matters
- Prevents system crashes due to full storage.
- Helps you decide when to delete or move files.
- Assists in planning upgrades or adding new storage.
- Useful for monitoring servers and shared systems.
Using the df Command to Check Disk Space
The most common way to check disk space on Linux is with the df command. It stands for "disk filesystem" and shows how much space is used and available on each mounted file system.
Basic Usage of df
Open your terminal and type:
df
This will display a list of mounted file systems with their total size, used space, available space, and mount points. However, the output is in blocks, which can be hard to read.
Making df Output Human-Readable
To see sizes in a more understandable format (like MB or GB), use the -h option:
df -h
This command shows sizes with units like K (kilobytes), M (megabytes), and G (gigabytes), making it easier to interpret.
Example Output
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 42% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sdb1 100G 60G 35G 64% /home
Useful df Options
-T: Shows the type of each file system.-i: Displays inode usage instead of disk space.--total: Adds a total line summarizing all file systems.
Try this command to see file system types:
df -hT
Checking Disk Usage with du
While df shows space usage per file system, the du command helps you check disk usage for specific directories or files. This is useful when you want to find out which folders are taking up the most space.
Basic du Usage
To check the size of a directory, run:
du -sh /path/to/directory
-s: Summarizes the total size.-h: Makes the output human-readable.
For example, to check your home directory size:
du -sh ~
Finding Large Directories
You can list the sizes of all subdirectories in the current folder with:
du -h --max-depth=1
This shows the size of each folder one level deep, helping you spot large directories quickly.
Sorting Disk Usage
To find the biggest directories, combine du with sort:
du -h --max-depth=1 | sort -hr
This sorts the directories by size in descending order.
Using Graphical Tools to Check Disk Space
If you prefer a graphical interface, many Linux distributions include disk usage analyzers. These tools provide visual representations of your disk space, making it easier to understand.
GNOME Disk Usage Analyzer (Baobab)
- Available on GNOME-based systems.
- Scans your file system and shows a pie chart or treemap.
- Helps identify large files and folders visually.
To open it, search for "Disk Usage Analyzer" in your applications menu or run:
baobab
KDE Partition Manager
- For KDE desktop users.
- Provides detailed information about partitions and free space.
- Allows you to manage partitions safely.
Other Tools
- ncdu: A terminal-based disk usage analyzer that is interactive and easy to use.
- KDirStat or QDirStat: Graphical tools similar to Baobab but with more features.
Checking Disk Space on Specific Partitions
Sometimes you want to check disk space on a particular partition or mount point. You can specify it with the df command:
df -h /home
This shows disk usage only for the /home partition.
If you want to check a device directly, use:
df -h /dev/sda1
Make sure the device is mounted; otherwise, df won’t show its usage.
Monitoring Disk Space Automatically
For servers or systems where disk space is critical, setting up automatic monitoring helps avoid surprises.
Using cron and Scripts
You can create a script that runs df -h and emails you if disk space is low.
Example script snippet:
#!/bin/bash
THRESHOLD=90
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ $USAGE -gt $THRESHOLD ]; then
echo "Disk space is above $THRESHOLD%" | mail -s "Disk Space Alert" your@email.com
fi
Schedule it with cron to run daily.
Using Monitoring Tools
- Nagios, Zabbix, and Prometheus can monitor disk space and alert you.
- These tools provide dashboards and detailed reports.
Understanding Inodes and Their Usage
Disk space isn’t just about bytes; Linux also uses inodes to track files. If you run out of inodes, you can’t create new files even if you have free space.
Checking Inode Usage
Use the df command with the -i option:
df -i
This shows inode usage per file system.
Why Inodes Matter
- Systems with many small files can run out of inodes.
- Monitoring inode usage prevents errors like "No space left on device" even when disk space is available.
Tips for Managing Disk Space on Linux
Keeping your disk space healthy requires regular checks and cleanup. Here are some tips:
- Remove unnecessary files and old backups.
- Use
duto find and delete large files. - Clear package caches (
sudo apt cleanorsudo dnf clean all). - Uninstall unused applications.
- Use log rotation to manage log file sizes.
- Consider adding more storage if needed.
Conclusion
Now you know several ways to check disk space on Linux, from simple commands like df and du to graphical tools and monitoring solutions. These methods help you keep your system running smoothly by avoiding storage problems.
Regularly checking your disk space and managing files wisely can save you from unexpected issues. Whether you’re a casual user or a system administrator, these tips and tools will help you stay on top of your Linux storage needs.
FAQs
How do I check disk space on Linux using the terminal?
Use the df -h command to see disk space usage in a human-readable format. It shows total, used, and available space for all mounted file systems.
What is the difference between df and du?
df shows disk space usage per file system, while du reports the size of directories and files. Use df for overall space and du to find large folders.
Can I check disk space for a specific directory?
Yes, use du -sh /path/to/directory to see the total size of that directory in a readable format.
How do I monitor disk space automatically on Linux?
You can create scripts with df and schedule them with cron to send alerts. Alternatively, use monitoring tools like Nagios or Zabbix.
What are inodes, and why should I check them?
Inodes track files on Linux. Running out of inodes means you can’t create new files even if space is free. Check inode usage with df -i.
