Last active
March 11, 2017 13:45
-
-
Save itsthejb/0db54d522c17532959703345cc482420 to your computer and use it in GitHub Desktop.
Python script to return directories with sizes less than specified minimum
This file contains hidden or 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 sys | |
import os | |
from functools import reduce | |
def sized(source, size): | |
m = {} | |
def fileSize(root, path): | |
joined = os.path.join(root, path) | |
return 0 if os.path.islink(joined) else os.path.getsize(joined) | |
for root, dirs, files in os.walk(source, topdown = False): | |
# Size all the files here... | |
total = reduce((lambda acc, path: acc + fileSize(root, path)), files, 0) | |
# Add the (already sized) children | |
total = reduce((lambda acc, path: acc + m[os.path.join(root, path)]), dirs, total) | |
# Store for the root | |
m[root] = total | |
if m[source] < size: | |
print(source) | |
def usage(): | |
print("Usage: " + __file__ + " <lt byte size> <root path>") | |
print("Also supports stdin.") | |
exit(1) | |
if __name__ == "__main__": | |
size = 0 | |
try: | |
size = int(float(sys.argv[1])) | |
except: | |
usage() | |
if len(sys.argv) < 2: | |
usage() | |
if len(sys.argv) > 2 and os.path.isdir(sys.argv[2]): | |
source = sys.argv[2] | |
for path in os.listdir(source): | |
path = os.path.join(source, path) | |
if os.path.isdir(path): | |
sized(path, size) | |
else: | |
for path in sys.stdin: | |
path = path.rstrip() | |
if os.path.isdir(path): | |
sized(path, size) | |
else: | |
sys.stderr.write("Error: " + path + " is not a path\n") | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment