Created
July 14, 2025 16:13
-
-
Save PierceLBrooks/b3397402b5946fc66052eb8b9660c2c2 to your computer and use it in GitHub Desktop.
dirunify.py
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
# 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