Skip to content

Instantly share code, notes, and snippets.

@PierceLBrooks
Created July 14, 2025 16:13
Show Gist options
  • Save PierceLBrooks/b3397402b5946fc66052eb8b9660c2c2 to your computer and use it in GitHub Desktop.
Save PierceLBrooks/b3397402b5946fc66052eb8b9660c2c2 to your computer and use it in GitHub Desktop.
dirunify.py
# Author: Pierce Brooks
import os
import sys
import shutil
import hashlib
import logging
import traceback
def hashify(target):
sha = hashlib.sha256()
sha.update(target)
return str(sha.hexdigest())
directories = sys.argv[1:]
hashes = {}
for directory in directories:
if not (os.path.exists(directory)):
continue
for root, folders, files in os.walk(directory):
names = list(sorted(files))
for name in names:
try:
path = os.path.join(root, name)
descriptor = open(path, "rb")
salt = hashify(descriptor.read())
descriptor.close()
if not (salt in hashes):
hashes[salt] = path
else:
if (sys.flags.debug):
print(path)
except:
logging.error(traceback.format_exc())
for key in hashes:
source = hashes[key]
destination = os.path.join(os.getcwd(), os.path.basename(source))
if not (os.path.exists(destination)):
try:
shutil.copy2(source, destination)
except:
logging.error(traceback.format_exc())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment