Skip to content

Instantly share code, notes, and snippets.

@gornostal
Created August 21, 2012 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gornostal/3416721 to your computer and use it in GitHub Desktop.
Save gornostal/3416721 to your computer and use it in GitHub Desktop.
cleanup.py
#!/usr/bin/env python
import glob
import os
import sys
if len(sys.argv) < 2:
print "Usage: ./cleanup.py path/to/dir/ [max_dir_size_in_gb]"
sys.exit()
max_dir_size = len(sys.argv) > 2 and int(sys.argv[2]) or 2 # 2Gb by default
max_dir_size *= 1024 ** 3
files = filter(os.path.isfile, glob.glob(sys.argv[1] + '*'))
files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
files = [(f, os.path.getsize(f)) for f in files]
dir_size = sum([int(size) for f, size in files])
while dir_size > max_dir_size and files:
f, size = files.pop()
os.remove(f)
dir_size -= size
print 'remove ' + f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment