Knowing how to check directory sizes in Linux is essential for managing disk space and keeping your system organized. Linux provides several built-in tools and commands to analyze storage usage and identify which directories or files are consuming the most space. Whether you are managing a small workstation or a large server, understanding these commands can help optimize performance and prevent storage issues.
1. Using the du
Command
The du
(Disk Usage) command is the most common and powerful tool to check directory sizes. It reports the disk space used by files and directories.
To check the size of a specific directory, run:
du -sh /path/to/directory
Options explained:
-s
: Displays only the total size.-h
: Shows human-readable output (KB, MB, GB).
Example:
du -sh /home/user/Documents
This command displays the total size of the “Documents” folder in an easy-to-read format.
To list the sizes of all subdirectories:
du -h --max-depth=1 /home/user
This displays the size of each folder within /home/user
, helping you locate large directories quickly.
2. Checking Total Disk Usage
If you want to see how much space is being used on the entire system, use:
df -h
The df
(Disk Filesystem) command shows mounted filesystems, their total size, used space, and available space. It’s ideal for an overview of disk capacity and helps prevent running out of storage on critical partitions.
3. Sorting Directories by Size
To find the largest directories on your system, combine commands:
du -h /path | sort -hr | head -10
This lists the top 10 largest directories in descending order. It’s a quick way to pinpoint which folders are consuming the most disk space.
4. Using the ncdu
Command
For a more interactive way to analyze directory sizes, install ncdu (NCurses Disk Usage):
sudo apt install ncdu
Then run:
ncdu /path/to/directory
ncdu
opens a graphical, text-based interface in the terminal, allowing you to navigate through directories and delete unnecessary files directly from the interface.
5. Checking Directory Size with ls
and du
Combination
You can combine ls
and du
to analyze directories more effectively:
ls -lh
This lists files with their sizes in human-readable format. To combine both commands for more detailed output:
du -ch /path/to/directory/*
This shows the size of each file and a cumulative total at the end.
6. Using find
for Targeted Directory Analysis
If you want to find all files over a certain size, use:
find /path -type f -size +100M -exec ls -lh {} \;
This locates files larger than 100 MB. It’s useful for cleaning up old backups or large media files that consume space unnecessarily.
7. Automating Directory Size Checks
System administrators often automate disk checks using cron jobs. For example, create a cron task that logs directory sizes daily:
0 2 * * * du -sh /var/www >> /var/log/disk_usage.log
This command runs every night at 2 a.m. and saves size data to a log file for tracking.
8. Common Use Cases
- Monitor web servers: Identify growing logs or cache directories in
/var/www
. - Check user directories: Manage space in
/home
for multiple users. - Analyze system directories: Find large backups or temporary files in
/var
and/tmp
.
10. Conclusion
Efficient disk management is crucial for keeping your Linux system stable and fast. Commands like du
, df
, and ncdu
help you analyze storage, identify large files, and clean up space when needed. By regularly checking directory sizes, you ensure that your system runs smoothly and storage usage stays under control.