【问题标题】:Bash script that remove duplicates删除重复项的 Bash 脚本
【发布时间】:2022-10-23 07:47:36
【问题描述】:

这工作不正确。该脚本应该只删除副本,但该脚本会删除所有文件。

#!/bin/bash

DIR=$1

if [[ -z "$DIR" ]]; then
    echo "Error: files dir is undefined"
fi

files="$( find ${DIR} -type f )"

for file1 in $files; do 
    for file2 in $files; do
        if cmp -s "$file1" "$file2"; then
            rm $file2
        fi
    done
done

【问题讨论】:

  • 您必须确保[[ $file1 != "$file2" ]]。另外:如果文件名包含空格,您的脚本将不起作用。

标签: bash


【解决方案1】:

在超级用户上找到 this:

#!/bin/bash
declare -A arr
shopt -s globstar

for file in **; do
  [[ -f "$file" ]] || continue
   
  read cksm _ < <(md5sum "$file")
  if ((arr[$cksm]++)); then 
    echo "rm $file"
  fi
done

【讨论】:

    【解决方案2】:

    我找到答案:

    #!/bin/bash
    
    DIR=$1
    
    if [[ -z "$DIR" ]]; then
        echo "Error: files dir is undefined"
    fi
    
    files="$( find ${DIR} -type f )"
    
    for file1 in $files; do 
        for file2 in $files; do
            if cmp -s "$file1" "$file2"; then
                rm $file2
            fi
        done
    done
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 2015-05-27
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 2016-12-28
      • 2018-06-16
      相关资源
      最近更新 更多