0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

指定ディレクトリ階層内に存在する重複ファイルを検索(ファイル名およびサイズが等しい条件)

Last updated at Posted at 2018-10-10

こんにちは。
指定ディレクトリ階層内に存在する重複ファイルの存在を検索しました。

ファイル名が等しいファイルの存在を検索

ディレクトリ名、ファイル名が出力されます。GNU find, uniq を利用し(macOS では homebrew でインストール可能)、見やすくするために、fdupes コマンドに似せて、重複グループ間を空行で区切っています1

$ brew install findutils coreutils
$ gfind . -type f -printf '%h %f\n' | sort -k2 | LC_ALL='C' guniq -f1 --all-repeated=separate 
. memo.txt
./memo memo.txt

. memo2.txt
./memo memo2.txt

ファイル名およびサイズが等しいファイルの存在を検索

ディレクトリ名、ファイル名、サイズが出力されます。

$ brew install findutils coreutils
$ gfind . -type f -printf '%h %f %s\n' | sort -k2 -k3n | LC_ALL='C' guniq -f1 --all-repeated=separate
. memo.txt 144
./memo memo.txt 144

. memo2.txt 288
./memo memo2.txt 288

md5 値もしくはsha256 値が等しいファイルの存在を検索

md5 値(もしくは sha256 値)を計算すればより厳密に重複判定できます。ただし計算コストが増え実行時間を要します。(対策案として、ファイルサイズだけで候補を絞り込んだ後に計算するようにすれば無駄を避けることができます。)

  • md5 値が等しいファイルの存在を検索
$ brew install md5sha1sum
$ gfind . -type f -printf '%p %s ' -exec md5sum {} \; | awk '{$NF="";print}' | sort -k2n -k3 | LC_ALL='C' guniq -f1 -D
./memo.txt 144 44505edf323152d0e55e52440543bbde
./memo/memo.txt 144 44505edf323152d0e55e52440543bbde
  • sha256 値が等しいファイルの存在を検索
$ brew install md5sha1sum
$ gfind . -type f -printf '%p %s ' -exec shasum -a 512 {} \; | awk '{$NF="";print}' | sort -k2n -k3 | LC_ALL='C' guniq -f1 -D
./memo.txt 144 311b7835fb22f48b838f324a09d13244ee83a92fffec8d53f9122247f2bcb6a63de8b706d4403871f703280821694065b33469cea512cd55864a3d218751c0af
./memo/memo.txt 144 311b7835fb22f48b838f324a09d13244ee83a92fffec8d53f9122247f2bcb6a63de8b706d4403871f703280821694065b33469cea512cd55864a3d218751c0af
  1. fdupesを自作してみた」と言う記事も参考になります。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?