Created
August 21, 2012 15:46
-
-
Save gornostal/3416721 to your computer and use it in GitHub Desktop.
cleanup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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