Measuring Directory Size in Linux: A Complete Guide
In Linux, it’s often important to know how much space a directory consumes. Whether you are managing servers, cleaning up development projects, or troubleshooting disk usage, being able to measure directory size quickly is a valuable skill.
The most reliable way to check this is by using the du
(disk usage) command, along with a few helpful options.
Checking the Total Size of a Directory
The simplest command to find the total size of any directory is:
du -sh /path/to/directory
du
: Short for disk usage.-s
(summary): Shows only the total size of the directory, not every single file.-h
(human readable): Displays results in KB, MB, or GB instead of raw bytes.
👉 Example output:
2.5G /path/to/directory
This means the folder takes about 2.5 GB of disk space.
Viewing Sizes of Subdirectories
Sometimes you need to see which subdirectories are taking up the most space. In that case, run:
du -h --max-depth=1 /path/to/directory
This lists the size of each immediate subdirectory, plus the total at the end. For example:
500M /path/to/directory/logs
1.2G /path/to/directory/data
800M /path/to/directory/backups
2.5G /path/to/directory
From this output, you can quickly identify the largest subfolders.
Hidden Files and Folders
Linux directories often contain hidden files (those starting with a dot, e.g., .git
, .cache
). The du
command already includes these by default in its calculations, so you don’t need extra options to account for them.
If you want to specifically analyze hidden folders, you can run:
du -h --max-depth=1 /path/to/directory/.*
Alternative Tools
Besides du
, there are other ways to measure and analyze disk usage:
ncdu
(NCurses Disk Usage) – an interactive tool that makes it easier to navigate large directories:
sudo apt install ncdu # Debian/Ubuntu
ncdu /path/to/directory
With this, you can browse directories in a terminal interface and delete files directly.
ls
with sorting – good for individual file sizes, not directory totals:
ls -lhS /path/to/directory
Key Considerations
- Log files: System logs can grow quickly. Regular cleanup or rotation helps keep them manageable.
- Backups and archives: Large tarballs or backup directories often take up significant space and can sometimes be moved to external storage.
- Cache directories: Temporary cache folders may be safely cleared, depending on the application.
- Version control history: Repositories with large binary files may accumulate hidden storage in
.git
folders. Running garbage collection (git gc
) can reduce size.
Finally
Measuring directory size in Linux is a fundamental skill for system maintenance and project management. With du -sh
, you can get a quick overview, while du -h --max-depth=1
allows you to explore subdirectory sizes. For more detailed exploration, ncdu
provides an interactive experience.
By keeping an eye on hidden files, logs, caches, and backups, you ensure your system stays efficient and avoids sudden “disk full” surprises.
Comments ()