Skip to content

Instantly share code, notes, and snippets.

@maedoc
Last active August 2, 2018 09:23
Show Gist options
  • Save maedoc/4ff375204003ab57fa01dd2815583142 to your computer and use it in GitHub Desktop.
Save maedoc/4ff375204003ab57fa01dd2815583142 to your computer and use it in GitHub Desktop.
Print GB months, GB and inode usage
#!/usr/bin/env python3
import os
import time
import sys
path, gbmo_limit = sys.argv[1:]
gbmo_limit = float(gbmo_limit)
now = time.time()
s_per_mo = (3600 * 144 * 52) / 12.0
root_sums = {}
root_gb = {}
root_inodes = {}
root_dirs = {}
for root, dirs, files in os.walk(path, topdown=False):
sum = 0
gbs = 0
inodes = len(files) + len(dirs)
for file in files:
fullfile = os.path.join(root, file)
try:
stat = os.stat(fullfile)
except (FileNotFoundError, PermissionError):
continue
mo = (now - stat.st_ctime) / s_per_mo
gb = stat.st_size / 2**30
sum += gb * mo
gbs += gb
for dir in dirs:
root_dir = os.path.join(root, dir)
if root_dir in root_sums:
sum += root_sums[root_dir]
gbs += root_gb[root_dir]
inodes += root_inodes[root_dir]
root_sums[root] = sum
root_gb[root] = gbs
root_inodes[root] = inodes
root_dirs[root] = dirs[:]
if sum > gbmo_limit:
print('%03.2f\t%03.2f\t%03.1fk\t%s' % (sum, gbs, inodes/1000, root))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment