微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 bash 中,如何删除同一文件的多个版本?

如何解决在 bash 中,如何删除同一文件的多个版本?

这可能是一个非常特殊的案例,但我对 bash 知之甚少,我需要删除“重复”文件。过去几天我一直在下载完全合法的电子游戏 rom,我注意到很多包都有同一游戏的许多不同版本,如下所示:

Awesome Golf (1991).lnx
Awesome Golf (1991) [b1].lnx
Baseball Heroes (1991).lnx
Baseball Heroes (1991) [b1].lnx
Basketbrawl (1992).lnx
Basketbrawl (1992) [a1].lnx
Basketbrawl (1992) [b1].lnx
Batman Returns (1992).lnx
Batman Returns (1992) [b1].lnx

如何制作一个删除重复项的 bash 脚本?副本是具有相同名称的任何文件名称将是第一个括号之前的字符串。该脚本应该解析所有文件获取它们的名称,查看哪些名称匹配以检测重复项,并删除除第一个文件之外的所有文件(第一个是按字母顺序出现的第一个)。

解决方法

请您尝试以下操作:

#!/bin/bash

dir="dir"                               # the directory where the rom files are located
declare -A seen                         # associative array to detect the duplicates
while IFS= read -r -d "" f; do          # loop over filenames by assigning "f" to it
    name=${f%(*}                        # extract the "name" by removing left paren and following characters
    name=${name%.*}                     # remove the extension considering the case the filename doesn't have parens
    name=${name%[*}                     # remove the left square bracket and following characters considering the case as above
    name=${name%% }                     # remove trailing whitespaces,if any
    if (( seen[$name]++ )); then        # if the name duplicates...
        # remove "echo" if the output looks good
        echo rm -- "$f"                 # then remove the file
    fi
done < <(find "$dir" -type f -name "*.lnx" -print0 | sort -z -t "." -k1,1)
                                        # sort the list of filenames in alphabetical order
  • 请将第一行 dir= 修改为包含 rom 文件的目录路径。
  • echo 命令只是打印要删除的文件名作为排练。如果输出看起来不错,则删除 echo 并执行真正的。

[说明]

  • 关联数组 seen 将提取的“名称”与 外观计数器。如果计数器不为零,则文件是重复的 一个,可以删除(只要文件正确排序)。
  • -print0find 选项、-zsort 选项和 -d "" read 选项将空字符作为文件名的分隔符 接受包含特殊字符(如空格)的文件名, 制表符、换行符等

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。