Skip to content

Instantly share code, notes, and snippets.

@bschlenk
Created December 26, 2016 08:07
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 bschlenk/69ce06a9aa24ecbeabcd697554c885c7 to your computer and use it in GitHub Desktop.
Save bschlenk/69ce06a9aa24ecbeabcd697554c885c7 to your computer and use it in GitHub Desktop.
Reorganize a directory into subfolders the same way git organizes its object files.
#!/usr/bin/env python3
"""
Reduce the size of a directory by creating new directories for each
unique combination of the first two letters of the existing files.
This is the same way that git stores object files.
For example, a file with this name would result in the following:
fff394e6-214d-4f74-8607-d6825f17c8fd
|
V
ff/f394e6-214d-4f74-8607-d6825f17c8fd
This has the potential to reduce the root directory size by a factor of 256 (assuming file names are in hex).
"""
import os
import sys
import shutil
from os.path import join
directories = set()
parent = sys.argv[1]
for fname in os.listdir(parent):
prefix = fname[:2]
newname = fname[2:]
newdir = join(parent, prefix)
if prefix not in directories:
os.makedirs(newdir, exist_ok=True)
directories.add(prefix)
print(f'created directory {prefix}')
shutil.move(join(parent, fname), join(newdir, newname))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment