使用腳本查找並刪除 osx 中的重複文件

使用腳本查找並刪除 osx 中的重複文件

從:http://www.chriswrites.com/2012/02/how-to-find-and-delete-duplicate-files-in-mac-os-x/ 如何修改它以僅刪除它看到的文件的第一個版本。

從 Spotlight 或 Utilities 資料夾中開啟終端 使用 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 {} \;

其次,列印除第一個副本之外的所有副本的一種方法是使用輔助文件,比方說/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

您也可以使用建議的重複檔案刪除器自動刪除重複項這個帖子

相關內容