Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Tracking Down Where Disk Space Has Gone on Linux
As a Linux user, you might have come across a situation where you ran out of disk space, but you are not sure where all space has gone. It can be frustrating, but fortunately, there are several tools and techniques you can use to track down where disk space has gone on Linux.
In this article, we will cover the following methods to help you identify where your disk space has gone and reclaim valuable storage space.
Check Overall Disk Usage
The first step is to get an overview of your disk usage. The df command shows filesystem usage across all mounted partitions:
df -h
This displays human-readable output showing used and available space for each filesystem. To check disk usage of specific directories, use the du command:
du -sh /path/to/directory
The -s option shows the total size of the directory, while -h makes the output human-readable. To see disk usage of all directories in the current location:
du -h --max-depth=1 | sort -hr
The sort -hr command sorts the output by size in descending order, making it easy to identify the largest directories.
Identify Large Files
Once you have identified directories consuming the most space, locate large files within them using the find command. To find all files larger than 100MB:
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
For a more comprehensive search, you can find the top 20 largest files on your system:
find / -type f -exec du -Sh {} + 2>/dev/null | sort -rh | head -20
Check System Logs
Log files can consume significant disk space, especially on active systems. Check the size of common log directories:
sudo du -sh /var/log/*
Common problematic log files include syslog, kern.log, and application-specific logs. To view the largest log files:
sudo find /var/log -type f -size +50M -exec ls -lh {} \;
You can safely truncate large log files or configure log rotation using logrotate to prevent future issues.
Check Package Cache
Package managers store downloaded packages in cache directories. For Debian/Ubuntu systems using APT:
du -sh /var/cache/apt/archives/ sudo apt-get clean
For Red Hat/CentOS systems using YUM/DNF:
du -sh /var/cache/yum/ sudo yum clean all
Check Trash and Temporary Files
Deleted files may still occupy space in the trash directory:
du -sh ~/.local/share/Trash/ rm -rf ~/.local/share/Trash/*
Check and clean temporary directories:
du -sh /tmp/ /var/tmp/ sudo rm -rf /tmp/* /var/tmp/*
Use Interactive Disk Usage Tools
For a more user-friendly approach, use ncdu (NCurses Disk Usage), an interactive command-line tool:
sudo apt-get install ncdu ncdu /
Navigate through directories using arrow keys and press Enter to explore subdirectories. Press d to delete files directly from the interface.
Check User Home Directories
Individual user directories can grow large over time:
sudo du -sh /home/* | sort -hr
Find Hidden Files and Directories
Hidden files (starting with a dot) can consume significant space:
du -sh .[^.]* * 2>/dev/null | sort -hr
Advanced Techniques
For comprehensive analysis, combine multiple commands to create a detailed disk usage report:
sudo find / -type f -size +500M 2>/dev/null | xargs du -sh | sort -hr > large_files.txt
Use lsof to find files that are deleted but still held open by processes:
sudo lsof +L1
Graphical Tools
For desktop users, graphical disk usage analyzers provide visual representations:
Baobab (Disk Usage Analyzer) Default in GNOME environments
K4DirStat KDE-based disk usage analyzer
QDirStat Cross-platform Qt-based analyzer
Conclusion
Tracking down disk space usage on Linux involves systematic checking of directories, files, logs, caches, and temporary storage. Using tools like du, find, and ncdu provides comprehensive insights into space consumption. Regular maintenance of logs, package caches, and temporary files prevents future disk space issues and keeps your system running efficiently.
