Created
December 26, 2016 08:07
-
-
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.
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 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