Skip to content

Instantly share code, notes, and snippets.

@nyango
Last active August 29, 2015 14:08
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 nyango/b98d707305dc01fc5737 to your computer and use it in GitHub Desktop.
Save nyango/b98d707305dc01fc5737 to your computer and use it in GitHub Desktop.
md5sumを用いて重複ファイルを削除するスクリプトを書いた / Using md5sum, this script removes duplicated files
#!/bin/bash
# this script removes duplicated files
# using md5sum.
#
# $1 : target directory
#
tmptable=$(mktemp temp.XXXXX)
\ls -t ${1%/}/* | while read line
do
md5 "$line" | sed "s,^MD5 (,," | sed "s/) = / /"
done > $tmptable
# duplicate check
cat $tmptable | awk '{print $NF}' | sort | uniq -c \
| awk '{if($1 > 1) print $NF}' \
| while read ff
do
cat $tmptable | egrep " $ff$" | sed "s/ $ff$//" | awk 'NR>1{print}'
done | while read rmfile
do
echo "removed "$rmfile
rm $rmfile
done
rm $tmptable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment