스크립트를 사용하여 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 {} \;)

둘째, 첫 번째 사본을 제외한 모든 사본을 인쇄하는 한 가지 방법은 보조 파일을 사용하는 것입니다 /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. 어떤 파일을 보관할지 묻는 메시지가 표시됩니다. 대신 추가하면 -dNfdupes는 항상 첫 번째 파일을 유지하고 다른 파일을 삭제합니다.

답변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

또한 제안된 중복 파일 제거 프로그램을 사용하여 중복 파일을 자동으로 삭제할 수도 있습니다.이 게시물.

관련 정보