A Python CLI tool to analyze and report directory sizes with threshold alerts. This tool helps you monitor disk usage and identify large directories that may need attention.
Related to Issue: #1008
- 📊 Calculate total directory size recursively
- 🚨 Alert when directory size exceeds a specified threshold
- 📝 Human-readable size formatting (B, KB, MB, GB, TB, PB)
- 📁 List subdirectory sizes with sorting by size
- 🔝 Display top N largest subdirectories
- 🛡️ Handles permission errors gracefully
- ⚡ Fast and efficient directory scanning
- Python 3.6+
- No external dependencies (uses only standard library)
- Clone this repository or download the script:
git clone https://github.com/sumanth-0/100LinesOfPythonCode.git
cd 100LinesOfPythonCode/#1008_Directory_Size_Monitor- Make the script executable (optional):
chmod +x directory_size_monitor.pyAnalyze a directory:
python directory_size_monitor.py /path/to/directorySet a threshold and get alerts if exceeded:
python directory_size_monitor.py /path/to/directory -t 500MB
python directory_size_monitor.py /path/to/directory --threshold 1.5GBList all subdirectories with their sizes:
python directory_size_monitor.py /path/to/directory -r
python directory_size_monitor.py /path/to/directory --recursiveDisplay only the top N largest subdirectories:
python directory_size_monitor.py /path/to/directory -n 10
python directory_size_monitor.py /path/to/directory --top 5python directory_size_monitor.py ~/Documents -t 2GB -n 10| Argument | Short | Description |
|---|---|---|
directory |
- | Directory path to analyze (required) |
--threshold |
-t |
Size threshold (e.g., 100MB, 1.5GB) |
--recursive |
-r |
Show all subdirectory sizes |
--top |
-n |
Show top N largest subdirectories |
The threshold can be specified with the following units:
B- BytesKB- Kilobytes (1024 bytes)MB- Megabytes (1024 KB)GB- Gigabytes (1024 MB)TB- Terabytes (1024 GB)
Examples: 500MB, 1.5GB, 100KB, 2TB
Analyzing: /home/user/Documents
----------------------------------------------------------------------
Total size: 3.45 GB (3,704,512,345 bytes)
Threshold: 2.00 GB
⚠️ WARNING: Directory size exceeds threshold!
Exceeded by: 1.45 GB
Subdirectories:
Photos 1.85 GB
Videos 1.20 GB
Projects 285.34 MB
Downloads 105.67 MB
Documents 15.23 MB
- 🔍 Disk Space Management: Identify directories consuming excessive disk space
- 📦 Backup Planning: Determine which directories need backup priority
- 🧹 Cleanup Operations: Find candidates for cleanup or archival
- 📊 System Monitoring: Set up regular checks with threshold alerts
- 🚀 Performance Optimization: Identify directories that might slow down operations
- ✅ Clean, readable code
- ✅ Proper error handling
- ✅ No external dependencies
- ✅ Under 100 lines of code
- ✅ PEP 8 compliant
This project is part of the 100 Lines of Python Code repository. Contributions, improvements, and bug reports are welcome!
This project follows the license of the parent repository.
Contributed as part of Issue #1008
Note: This tool handles permission errors gracefully. If you encounter "Permission denied" errors for certain directories, the tool will skip them and continue with accessible directories.