重複ファイルを削除し、最新のファイルのみを保持する

重複ファイルを削除し、最新のファイルのみを保持する

複数のファイルが重複しているがファイル名が異なっていたり、サブフォルダー内で失われている写真ダンプ フォルダーをクリーンアップしようとしています。

rmlint、duff、fdupes などのツールを調べましたが、最新のタイムスタンプを持つファイルのみを保持する方法が見つからないようです。結果を後処理する必要があると思いますが、どこから始めればよいのかさえわかりません。

重複ファイルのリストを取得し、最新のファイル以外のすべてを削除する方法を教えていただけますか?

答え1

zsh シェルを使用していることに注意してください。

次のようなことを試してみてください(未テスト、https://github.com/lipidity/btrfs-fun/blob/master/dedup):

# checksum everything in ${DIR}
cksums=$(mktemp)
find ${DIR} -xdev -type f -print0 | xargs -0 md5sum > $cksums

# loop through each md5 hash found
for hash in $(sort $cksums | uniq -w 32 -d | cut -c 1-32); do
  # list of files with this hash
  files=$(grep $hash $cksums | cut -c 35-)
  f=(${(f)files})
  unset files
  # $f now contains array of files with the same checksum
  # compare the first file to the rest, deleting any that are older
  newest=$f[1]
  for file in $f[2,-1]; do
    # make sure the files are still the same
    cmp $newest $file || continue
    # remove the older file
    if [[ $file -nt $newest ]]; then
      rm $newest
      newest=$file
    else
      rm $file
    fi
  done
done

テストは行われていませんが、大部分は解決できるはずです。さらに説明が必要な場合はお知らせください。

答え2

コマンドと各ファイルのファイル名をecho使用して生成されたチェックサムをチェックサムで並べ替えます。 を使用して、同じチェックサムを持つものが実際に重複していることを確認できます。sumcmp

関連情報