Introduction / Why This Is Needed
Disk space filling up is a common problem in Linux systems that can lead to slowdowns, application errors, and even system boot failures. Analyzing disk usage allows you to quickly identify which files and folders occupy the most space and take steps to clean them up. In this guide, you will master the essential tools for analyzing disk space: from simple commands to interactive utilities.
Requirements / Preparation
Before you begin, ensure that:
- You have access to a Linux terminal (via SSH or locally).
- For some commands (e.g., searching the entire system), superuser privileges (root or sudo) are required.
- Basic utilities are installed (they are usually present by default). If not, we will show how to install additional ones.
Step-by-Step Instructions
Step 1: Check Overall Disk Usage with df
The df (disk free) command shows the usage of each mounted filesystem partition. This gives a general idea of which partitions are nearly full.
df -h
The key flag -h (human-readable) outputs sizes in convenient units (KB, MB, GB). Pay attention to the Use% column—the usage percentage. If it is close to 100%, the partition needs to be cleaned.
Step 2: Analyze Folder Sizes with du
After identifying a full partition, you need to find which folders within it take up the most space. For this, use du (disk usage).
du -h --max-depth=1 /path/to/partition
For example, for the home directory:
du -h --max-depth=1 /home
This command will show the size of each first-level folder in /home. The --max-depth=1 parameter limits the depth to avoid cluttering the output. You can change the depth or specify a particular folder.
Step 3: Install ncdu for Interactive Analysis
For a more convenient analysis, install ncdu (NCurses Disk Usage). This is a console utility with an interactive interface that allows you to quickly navigate the filesystem and see sizes.
Installation:
Ubuntu/Debian:
sudo apt update && sudo apt install ncdu
CentOS/RHEL:
sudo yum install ncdu
Launch:
ncdu /path/to/partition
In the ncdu interface, you can navigate with arrow keys, press Enter to enter folders, and d to delete files (be careful!). This is an excellent way to explore disk usage in detail.
Step 4: Find the Largest Files in the System
Sometimes the problem lies in a few huge files (logs, caches, ISO images). To find them, use a combination of find, du, and sort.
sudo find / -type f -exec du -h {} + | sort -rh | head -n 20
Explanation:
sudo find /— search for files starting from the root (requires privileges).-type f— only files (not folders).-exec du -h {} +— for each file, output its size.sort -rh— sort in descending order (human-readable).head -n 20— take the top 20.
Be cautious: searching the entire disk can take a long time and create load. You can limit the path, for example, to /home.
Step 5: Use Graphical Tools if Necessary
If you are working in a graphical environment (GUI), there are convenient visualizers:
- Baobab (for GNOME): shows disk usage as a ring chart or tree.
Installation:
sudo apt install baobab(Ubuntu/Debian). Launch from the applications menu or withbaobab. - Filelight (for KDE): a similar utility that creates a clear disk map.
Installation:
sudo apt install filelight(Ubuntu/Debian) or via Discover.
These tools are especially useful for beginners.
Verifying the Result
After completing the steps, you should:
- Know which partitions are full (via
df). - Understand which folders occupy the most space (via
duorncdu). - Be able to locate specific large files (via
find). - Use graphical tools if necessary.
If you have freed up space, run df -h again to confirm that the usage percentage has decreased.
Potential Issues
- Access errors (Permission denied): Many commands, especially when searching from root, require
sudo. If you see errors, try addingsudo(but be careful with deletion). - Slow
findperformance on the entire disk: limit the path. For example, specify/homeor/varinstead of/. ncdunot installed: install it via the package manager as shown above.- Different space reporting:
dfshows space per filesystem (including reserved space), whiledushows actual usage. This is normal, and the difference can be 5–10% (reserved for root).
Additional Tips
- Regularly check logs in
/var/log—they can grow large. - Clean package caches:
sudo apt clean(Ubuntu/Debian) orsudo yum clean all(CentOS). - Remove old kernels (if there are many) via
dpkg --list | grep linux-imageandsudo apt purge linux-image-old-version. - Use
journalctl --disk-usageto check systemd journal usage andjournalctl --vacuum-size=100Mto clean it up.
These tips will help you keep disk space under control.