Last active
May 3, 2021 13:54
-
-
Save andywenk/8339ab03b7411d0dfa36958a8e9b4eeb to your computer and use it in GitHub Desktop.
dir-checksum - minimal Python script to create a checksum of all files in a directory
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
#! /usr/bin/env python | |
## Create the checksum of all the files in a directory | |
## find the original script here: https://unix.stackexchange.com/questions/35832/how-do-i-get-the-md5-sum-of-a-directorys-contents-as-one-sum | |
import hashlib, hmac, os, stat, sys | |
## Return the hash of the contents of the specified file, as a hex string | |
def file_hash(name): | |
f = open(name) | |
h = hashlib.sha256() | |
while True: | |
buf = f.read(16384) | |
if len(buf) == 0: break | |
h.update(buf) | |
f.close() | |
return h.hexdigest() | |
## Traverse the specified path and update the hash with a description of its | |
## name and contents | |
def traverse(h, path): | |
rs = os.lstat(path) | |
quoted_name = repr(path) | |
if stat.S_ISDIR(rs.st_mode): | |
h.update('dir ' + quoted_name + '\n') | |
for entry in sorted(os.listdir(path)): | |
traverse(h, os.path.join(path, entry)) | |
elif stat.S_ISREG(rs.st_mode): | |
h.update('reg ' + quoted_name + ' ') | |
h.update(str(rs.st_size) + ' ') | |
h.update(file_hash(path) + '\n') | |
else: pass # silently symlinks and other special files | |
h = hashlib.sha256() | |
for root in sys.argv[1:]: traverse(h, root) | |
h.update('end\n') | |
print h.hexdigest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment