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
How to Get the Size of a Directory in Linux?
In Linux, managing disk space effectively requires knowing how to determine directory sizes. A directory in Linux is similar to a folder in other operating systems, organizing files and subdirectories in a hierarchical structure. Directories are treated as files themselves, having attributes, permissions, and metadata such as creation and modification times.
Understanding directory sizes helps system administrators monitor disk usage, identify space-consuming files, and maintain optimal system performance. This article explores three practical methods to get directory sizes in Linux.
Method 1: Using the "du" Command
The "du" command stands for "disk usage" and is the most powerful tool for calculating directory sizes. It recursively scans directories and subdirectories, displaying the size of each item or providing summary information.
Basic Usage
du -h /path/to/directory
The -h flag displays sizes in human-readable format (KB, MB, GB). For a summary of the total directory size only, use:
du -sh /path/to/directory
Common Options
| Option | Description | Example |
|---|---|---|
-s |
Summary only (total size) | du -sh /home/user |
-a |
Include all files, even hidden ones | du -ah /home/user |
-c |
Display grand total at the end | du -hc /home/user/* |
--exclude |
Exclude specific directories | du -h --exclude=cache /home/user |
-x |
Stay within the same filesystem | du -hx /home/user |
Example Output
$ du -sh /home/user 2.4G /home/user $ du -h /home/user | head -5 120K /home/user/.config/gtk-3.0 8.0K /home/user/.config/pulse 1.2M /home/user/.config 456M /home/user/Documents 2.4G /home/user
Method 2: Using the "ls" Command with Filters
The "ls" command lists directory contents and can be combined with options to show file sizes. While less comprehensive than du, it's useful for quick directory content analysis.
Basic Directory Listing with Sizes
ls -lh /path/to/directory
This displays files with human-readable sizes. To sort by size (largest first):
ls -lhS /path/to/directory
Useful ls Options for Size Analysis
# Sort by size, largest first ls -lhS # Sort by size, smallest first ls -lhSr # Show only directories ls -ld */ # Show total size at the top ls -lh --block-size=M
Combining with Other Commands
# Find files larger than 50MB
find . -type f -size +50M -exec ls -lh {} \;
# List only .zip files with sizes
ls -lh *.zip
# Get total size of all files in current directory
ls -la | awk '{sum += $5} END {print "Total:", sum/1024/1024, "MB"}'
Method 3: Using GUI Tools
Linux provides several graphical tools for users who prefer visual representations of disk usage. These tools offer interactive interfaces with color-coded displays and intuitive navigation.
Popular GUI Tools
| Tool | Description | Installation |
|---|---|---|
| Baobab (Disk Usage Analyzer) | Tree map visualization, GNOME default | sudo apt install baobab |
| Filelight | Concentric circles display, KDE tool | sudo apt install filelight |
| QDirStat | Tree map with detailed statistics | sudo apt install qdirstat |
| KDirStat | Advanced features, file age analysis | sudo apt install kdirstat |
These tools can be launched from the applications menu or command line. Baobab is pre-installed on most GNOME-based distributions and provides an excellent starting point for visual disk usage analysis.
Comparison of Methods
| Method | Speed | Detail Level | Best For |
|---|---|---|---|
du command |
Fast | High (recursive) | Scripts, automation, precise measurements |
ls command |
Very Fast | Low (single level) | Quick checks, file-by-file analysis |
| GUI tools | Moderate | High (visual) | Interactive exploration, presentations |
Conclusion
Getting directory sizes in Linux can be accomplished through multiple methods, each serving different needs. The du command offers the most comprehensive and scriptable solution, while ls provides quick file-level information. GUI tools excel at visual exploration and interactive analysis, making them ideal for users who prefer graphical interfaces over command-line tools.
