使用脚本在osx中​​查找和删除重复文件


11

来自:http : //www.chriswrites.com/2012/02/how-to-find-and-delete-duplicate-files-in-mac-os-x/ 如何修改此内容以仅删除第一个版本的它看到的文件。

从Spotlight或“实用工具”文件夹中打开“终端”,然后使用cd命令切换到要从中搜索的目录(文件夹)(包括子文件夹)。在命令提示符下,键入cd,例如cd〜/ Documents,以将目录更改为您的主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

Answers:


4

首先,您必须重新排列第一个命令行的顺序,以便维持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

希望这可以帮助=)


39

另一种选择是使用fdupes:

brew install fdupes
fdupes -r .

fdupes -r .在当前目录下递归查找重复文件。添加-d删除重复项-系统将提示您保留哪些文件;如果改为添加-dN,则fdupes将始终保留第一个文件并删除其他文件。


7
fdupes太棒了!像魅力一样工作!多谢兄弟。!
racl101 2014年

3

我编写了一个脚本,用于重命名文件以匹配其内容的哈希。

它使用文件字节的子集,因此速度很快,如果发生冲突,它会在名称后添加一个计数器,如下所示:

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

这样一来,您就可以轻松地自行查看和删除重复项,而不必再对他人的照片信任他人的软件。

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

在此处输入图片说明


+1仅用于GIF显示!!
NoobEditor

0

这是通过Michael Tsai开发的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

您还可以自动删除重复的文件,重复卸妆中提出这个职位


1
(1)什么是“ EagleFiler”?它是macOS的一部分吗?如果没有,您从哪里得到?(2)那是不是意味着一长段代码(我固定它的方式)?(3)请修正您的缩进。(4)一个人到底该如何使用呢?
斯科特,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.