A fast and efficient Bash utility for analyzing and displaying directory sizes in human-readable format.
dir-sizes
is a streamlined directory size analyzer that calculates the total size of each subdirectory within a specified directory. It presents the results sorted by size in an easy-to-read format, making it simple to identify which directories are consuming the most disk space.
- Recursive Size Calculation: Accurately calculates the total size including all nested subdirectories
- Human-Readable Output: Automatically converts bytes to appropriate units (KB, MB, GB, etc.)
- Sorted Results: Displays directories sorted by size (smallest to largest) for easy analysis
- Permission Handling: Gracefully handles permission errors, showing warnings while continuing execution
- Fast Performance: Efficient implementation using native
du
command - Standards Compliant: Follows strict BASH coding standards for reliability and safety
- Security Hardened: Implements PATH locking, secure temp files, and proper signal handling
-
Clone this repository:
git clone https://github.com/username/dux.git cd dux
-
Make the script executable:
chmod +x dir-sizes
-
Optionally, add to your PATH:
# Add to ~/.bashrc or ~/.bash_profile export PATH="$PATH:/path/to/dux"
Or create a symlink:
sudo ln -s /path/to/dux/dir-sizes /usr/local/bin/dir-sizes
Basic usage:
dir-sizes [directory]
# Analyze current directory
dir-sizes
# Analyze specific directory
dir-sizes /var
# Analyze and show only the 10 largest directories
dir-sizes /usr | tail -10
Options:
-h, --help Display help message and exit
-V, --version Display version information and exit
Arguments:
directory Directory to analyze (defaults to current directory)
<size> <path>
Where:
<size>
is the human-readable size with IEC units (e.g., 128.5MiB, 1.2GiB)<path>
is the absolute or relative path to the directory
Example output:
0.0B ./.cache
56.7KB ./.git
102.6KB ./src
1.5MB ./docs
23.4MB ./data
- Uses
du -sb
to calculate actual disk usage in bytes - Includes all nested subdirectories in the size calculation
- Provides the total recursive size for each immediate subdirectory
- Permission errors are reported to stderr but don't stop execution
- Directories that can't be fully read show the size of accessible contents
- Large directories with many files may take time to analyze
- The script must recursively calculate sizes, which involves reading directory metadata
- For very large filesystems, consider analyzing specific subdirectories
0
- Success1
- General error (invalid directory, complete failure)22
- Invalid command-line option
- Bash 5.2 or higher (uses modern Bash features)
- GNU coreutils 8.32+ (for numfmt IEC units)
- GNU coreutils (
du
,cut
,sort
,numfmt
) - Standard POSIX utilities
The script follows strict BASH coding standards (v1.2.0 improvements):
- Uses
set -euo pipefail
withshopt -s inherit_errexit
for robust error handling - Implements proper variable scoping with typed declarations (
declare -i
,declare --
) - Secure temporary file handling with
mktemp
and automatic cleanup via traps - PATH security hardening to prevent command injection
- Signal handling for clean interruption (SIGINT, SIGTERM)
Feature | dir-sizes | du | ncdu | dust |
---|---|---|---|---|
Human-readable | ✓ (IEC) | With -h | ✓ | ✓ |
Sorted output | ✓ | With sort | ✓ | ✓ |
Interactive | ✗ | ✗ | ✓ | ✗ |
Recursive totals | ✓ | ✓ | ✓ | ✓ |
Security hardened | ✓ | ✗ | ✗ | ✗ |
Signal handling | ✓ | ✗ | ✓ | ✗ |
Dependencies | Minimal | None | ncurses | Rust |
Speed | Fast | Fast | Moderate | Fast |
If you see many "Permission denied" errors:
# Run with sudo for system directories
sudo dir-sizes /var
# Or suppress errors (not recommended, total sizes will be incorrect)
dir-sizes /var 2>/dev/null
For very large directories:
# Analyze specific subdirectories instead
dir-sizes /large/dir/specific-subdir
# Use timeout to limit execution time
timeout 30 dir-sizes /very/large/dir
# Basic functionality test
dir-sizes /tmp
# Test with various directories
for dir in /tmp /var/log /home; do
echo "Testing $dir:"
dir-sizes "$dir" 2>/dev/null | head -5
done
This project follows the organization's BASH coding standards as defined in BASH-CODING-STYLE, including:
- Proper shebang (
#!/bin/bash
) with script description - Critical safety settings (
inherit_errexit
,shift_verbose
,extglob
,nullglob
) - Consistent variable declarations with proper typing
- Standard utility functions (error handling, messaging)
- Comprehensive inline and usage documentation
- Security-first approach (PATH locking, mktemp usage)
This project is licensed under the GNU General Public License v3.0 (GPL-3.0) - see the LICENSE file for details.
- Built on GNU coreutils' robust
du
command - Follows best practices from the Bash scripting community
- Inspired by the need for a simple, fast directory size analyzer
du(1)
- The underlying disk usage commandncdu
- Interactive ncurses-based disk usage analyzerdust
- Modern du replacement written in Rustduf
- Modern df replacement with better UI