Skip to content

Instantly share code, notes, and snippets.

@ehedaya
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehedaya/e6d8af21512171a0ce98 to your computer and use it in GitHub Desktop.
Save ehedaya/e6d8af21512171a0ce98 to your computer and use it in GitHub Desktop.
Copy files with md5 duplicate check
if [[ $1 ]]
then
# make sure we have required argment
echo "Attempting to copy files to $1"
else
echo "No target destination specified"
exit
fi
echo "Building md5 log"
# Unix timestamp filename
log=/tmp/md5copylog-`date +%s`.md5
# Empty it, just in case
> $log
# Build our md5 list by looping through the files in the destination folder and hashing each
for f in $1/*;
do
# generate an md5 for this file
m=`md5 -q "$f"`
echo -n "."
# append it to the log
echo $m >> $log
done;
# now let's try to copy them
for f in *;
do
# generate an md5 for this file
m=`md5 -q "$f"`
# check if it's in the log file we built
if grep -q $m $log
then
echo "File $f exists in target, will not copy"
else
echo "File $f does not exist in target, will copy"
cp "$f" "$1"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment