Skip to content

Instantly share code, notes, and snippets.

@deltheil
Last active October 22, 2016 21:28
Show Gist options
  • Save deltheil/7232594 to your computer and use it in GitHub Desktop.
Save deltheil/7232594 to your computer and use it in GitHub Desktop.
Files and directory comparison with md5deep matching mode (see http://md5deep.sourceforge.net/start-md5deep.html#match).
# Create a reference folder with some data
mkdir foo
echo "hey" > foo/A.txt
echo "scm" > foo/B.txt
echo "git" > foo/C.txt
# Generate the list of hashes for each file
# -b = bare mode (strips any leading directory
# information from displayed filenames)
md5deep -b foo/* > hashes.txt
# Create a modified folder with some changes
# A.txt: modified
# C.txt: deleted
# D.txt: added
mkdir bar
echo "HEY" > bar/A.txt
echo "scm" > bar/B.txt
echo "svn" > bar/D.txt
# Find the positive matches with the -m option
# ~ find all unmodified files
md5deep -bm hashes.txt bar/*
# => B.txt
# Find the negative matches with the -x option
# ~ find all added or modified files
md5deep -bx hashes.txt bar/*
# => A.txt
# => D.txt
@deltheil
Copy link
Author

See also Python filecmp:

from filecmp import dircmp
cmp = dircmp("foo","bar")

# Identical files
cmp.same_files
# ['B.txt']

# Added or modified files
cmp.diff_files
# ['A.txt']
cmp.right_only
# ['D.txt']

# Print a user-friendly report
cmp.report()
# diff foo bar
# Only in foo : ['C.txt']
# Only in bar : ['D.txt']
# Identical files : ['B.txt']
# Differing files : ['A.txt']

Note: this performs byte-by-byte comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment