スクリプトを使用して OSX 内の重複ファイルを検索して削除する

スクリプトを使用して OSX 内の重複ファイルを検索して削除する

から:http://www.chriswrites.com/2012/02/how-to-find-and-delete-duplicate-files-in-mac-os-x/ 見つかったファイルの最初のバージョンのみを削除するようにこれを変更するにはどうすればよいですか。

Spotlight またはユーティリティ フォルダからターミナルを開きます。cd コマンドを使用して、検索するディレクトリ (フォルダ) (サブフォルダを含む) に変更します。コマンド プロンプトで cd と入力します (例: cd ~/Documents と入力して、ホーム ドキュメント フォルダにディレクトリを変更します)。コマンド プロンプトで、次のコマンドを入力します。

find . -size 20 \! -type d -exec cksum {} \; | sort | tee /tmp/f.tmp | cut -f 1,2 -d ' ' | uniq -d | grep -hif – /tmp/f.tmp > duplicates.txt

この方法では、単純なチェックサムを使用して、ファイルが同一かどうかを判断します。重複する項目の名前は、現在のディレクトリの duplicates.txt というファイルにリストされます。これを開くと、同一のファイルの名前が表示されます。重複を削除するには、さまざまな方法があります。テキスト ファイル内のすべてのファイルを削除するには、コマンド プロンプトで次のように入力します。

while read file; do rm "$file"; done < duplicates.txt

答え1

まず、find コマンドで見つかったファイルの順序が維持されるように、最初のコマンド ラインを並べ替える必要があります。

find . -size 20 ! -type d -exec cksum {} \; | tee /tmp/f.tmp | cut -f 1,2 -d ‘ ‘ | sort | uniq -d | grep -hif – /tmp/f.tmp > duplicates.txt

(注: 私のマシンではテスト目的で使用しましたfind . -type f -exec cksum {} \;)

次に、最初の重複を除くすべてを印刷する方法の 1 つは、補助ファイルを使用することです/tmp/f2.tmp。たとえば、次のようにします。

while read line; do
    checksum=$(echo "$line" | cut -f 1,2 -d' ')
    file=$(echo "$line" | cut -f 3 -d' ')

    if grep "$checksum" /tmp/f2.tmp > /dev/null; then
        # /tmp/f2.tmp already contains the checksum
        # print the file name
        # (printf is safer than echo, when for example "$file" starts with "-")
        printf %s\\n "$file"
    else
        echo "$checksum" >> /tmp/f2.tmp
    fi
done < duplicates.txt

/tmp/f2.tmpこれを実行する前に、たとえば次のコマンドを実行して、が存在し、空であることを確認してください。

rm /tmp/f2.tmp
touch /tmp/f2.tmp

これが役に立つことを願っています =)

答え2

別のオプションとしては、fdupes を使用することです。

brew install fdupes
fdupes -r .

fdupes -r .現在のディレクトリの下で重複ファイルを再帰的に検索します。-d重複を削除するには を追加します。どのファイルを保持するか尋ねられます。 の代わりに を追加すると-dN、fdupes は常に最初のファイルを保持し、他のファイルを削除します。

答え3

ファイルのコンテンツハッシュと一致するようにファイルの名前を変更するスクリプトを作成しました。

ファイルのバイトのサブセットを使用するので高速であり、衝突が発生した場合は次のように名前にカウンターが追加されます。

3101ace8db9f.jpg
3101ace8db9f (1).jpg
3101ace8db9f (2).jpg

これにより、必要以上に他人のソフトウェアに写真を預けることなく、自分で重複した写真を確認して削除することが簡単になります。

脚本: https://gist.github.com/SimplGy/75bb4fd26a12d4f16da6df1c4e506562

ここに画像の説明を入力してください

答え4

これは、によって開発されたEagleFilerアプリの助けを借りて行われます。マイケル・ツァイ

tell application "EagleFiler"

      set _checksums to {}
      set _recordsSeen to {}
      set _records to selected records of browser window 1
      set _trash to trash of document of browser window 1
      repeat with _record in _records
          set _checksum to _record's checksum
          set _matches to my findMatch(_checksum, _checksums, _recordsSeen)
          if _matches is {} then
              set _checksums to {_checksum} & _checksums
              set _recordsSeen to {_record} & _recordsSeen
          else
              set _otherRecord to item 1 of _matches
              if _otherRecord's modification date > _record's modification date 
then

            set _record's container to _trash
            else
                set _otherRecord's container to _trash
                set _checksums to {_checksum} & _checksums
                set _recordsSeen to {_record} & _recordsSeen
            end if
        end if
    end repeat
end tell

on findMatch(_checksum, _checksums, _recordsSeen)

    tell application "EagleFiler"
        if _checksum is "" then return {}
        if _checksums contains _checksum then
            repeat with i from 1 to length of _checksums
                if item i of _checksums is _checksum then
                    return item i of _recordsSeen
                end if
            end repeat
        end if
        return {}
    end tell

end findMatch

重複ファイル削除ツールで重複ファイルを自動的に削除することもできます。この郵便受け

関連情報