Skip to content

Instantly share code, notes, and snippets.

@bochoven
Created June 3, 2017 05:37
Show Gist options
  • Save bochoven/591fadd4567a5c43c0d90498a5378a0d to your computer and use it in GitHub Desktop.
Save bochoven/591fadd4567a5c43c0d90498a5378a0d to your computer and use it in GitHub Desktop.
macOS: Get all local users and calculate the size of the homedirectory
#!/usr/bin/python
# Get all local users and calculate the size of the homedirectory
import os, pwd, plistlib
system_accounts = ['root', 'daemon', 'nobody', 'Guest']
account_list = []
def get_size(start_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
try:
total_size += os.path.getsize(fp)
except Exception as e:
pass
return total_size
# Round to .5GB precision (29 bit)
def _HalfGB(size):
return size >> 29 << 29
for p in pwd.getpwall():
if p[0][:1] == '_':
continue
if p[0] in system_accounts:
continue
if p[5] == '/var/empty':
continue
account_list.append({
'name': p[0],
'homedir': p[5],
'size': str(_HalfGB(get_size(p[5])))
})
print plistlib.writePlistToString(account_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment