年末だし、ハードディスクの中の重複したファイルを見つけて、削除してしまおう。
対象とするファイル
- ファイルサイズが大きめ
- 不完全なファイルも存在する
- ディレクトリ階層を再帰的に検索
そのため、先頭1MB を対象としたハッシュ比較により、ファイルの同一性を検出します。
スクリプト
~/test.sh| #!/bin/sh
 cp /dev/null /tmp/list0.txt
 find . -type f -exec dd if={} bs=1024 count=1000 of=/tmp/work.bin \; -exec md5sum /tmp/work.bin >> /tmp/list0.txt \; -exec du {}  >> /tmp/list0.txt \;
 sed ':loop; N; $!b loop; ;s/\/tmp\/work.bin\n//g' /tmp/list0.txt > /tmp/list1.txt
 sort /tmp/list1.txt | uniq -D -w 32  > /tmp/list2.txt
 rm /tmp/work.bin /tmp/list0.txt
 
 | 
 
スクリプトの実行
| $ cd /PATH/TO/TARGET$ time ~/test.sh
 
 | 
実行結果
| 出力されるファイル | 内容 | 
| /tmp/list1.txt | ハッシュとサイズとパス | 
| /tmp/list2.txt | 重複しているファイルの一覧 | 
ファイルの削除
ファイルのサイズとディレクトリを参照して冗長なファイルを削除します。