Lists every file under a directory that's at or above a given size threshold. Use it to hunt down what's eating disk space or bloating a repo/build output.
#!/usr/bin/env python3
"""List files in a directory over a given size threshold (bytes)."""
import sys
from pathlib import Path
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit("usage: find-large-files.py <dir> <min_bytes>")
root = Path(sys.argv[1]).resolve()
try:
threshold = int(sys.argv[2])
except ValueError:
sys.exit("min_bytes must be an integer")
if not root.is_dir():
sys.exit(f"not a directory: {root}")
for p in sorted(root.rglob("*")):
if p.is_file() and p.stat().st_size >= threshold:
print(f"{p.stat().st_size:>12} {p}")
| dir | string — positional argument, directory to scan recursively, e.g. . |
|---|---|
| min_bytes | integer — positional argument, minimum file size to report, e.g. 10485760 (10 MB) |
dir
isn't a directory or min_bytes isn't an integer.
$ python3 find-large-files.py . 10485760
52428800 /repo/build/app.tar.gz
15728640 /repo/data/dump.sql
pathlib for all path handling — no shell globbing, no os.system.int() conversion instead of letting a bad value crash with a raw traceback.