删除Unix中的重复文件

问题描述:

我希望能够删除重复的文件,同时创建一个符号链接去除重复行。到目前为止,我可以显示重复的文件,问题是删除和删除。因为我要保留一个副本删除Unix中的重复文件

find "[email protected]" -type f -print0 | xargs -0 -n1 md5sum | sort --key=1,32 | uniq -w 
32 -d --all-repeated=separate 

输出

1463b527b1e7ed9ed8ef6aa953e9ee81 ./tope5final 
1463b527b1e7ed9ed8ef6aa953e9ee81 ./Tests/tope5 

2a6dfec6f96c20f2c2d47f6b07e4eb2f ./tope3final 
2a6dfec6f96c20f2c2d47f6b07e4eb2f ./Tests/tope3 

5baa4812f4a0838dbc283475feda542a ./tope1bfinal 
5baa4812f4a0838dbc283475feda542a ./Tests/tope1b 

69d7799197049b64f8675ed4500df76c ./tope3afinal 
69d7799197049b64f8675ed4500df76c ./Tests/tope3a 

945fe30c545fc0d7dc2d1cb279cf9c04 ./Tests/butter6 
945fe30c545fc0d7dc2d1cb279cf9c04 ./Tests/tope6 

98340fa2af27c79da7efb75ae7c01ac6 ./tope2cfinal 
98340fa2af27c79da7efb75ae7c01ac6 ./Tests/tope2c 

d15df73b8eaf1cd237ce96d58dc18041 ./tope1afinal 
d15df73b8eaf1cd237ce96d58dc18041 ./Tests/tope1a 

d5ce8f291a81c1e025d63885297d4b56 ./tope4final 
d5ce8f291a81c1e025d63885297d4b56 ./Tests/tope4 

ebde372904d6d2d3b73d2baf9ac16547 ./tope1cfinal 
ebde372904d6d2d3b73d2baf9ac16547 ./Tests/tope1c 

在这种情况下,例如我想删除./tope1cfinal并保持与./Tests/tope1c。删除后,我也想创建一个名称/ tope1cfinal指向/ Tests/tope1c的符号链接。

+0

1.为什么要删除'tope1cfinal'而不是'测试/ tope1c'? 2.为什么你想要一个符号链接而不是硬链接? (这两个问题有某种相关性:硬链接会使过程更加对称)。 – 2015-02-09 10:26:44

+0

一个符号链接将指向原始文件。因为某些程序仍然想访问原始删除的文件,符号链接就足够了 – Alexander 2015-02-09 11:47:23

+0

这并不能解释为什么您更喜欢通过硬链接进行符号链接。并且这并不回答问题1. – 2015-02-09 11:48:47

一种可能性:创建一个关联数组,其关键字是md5sum,其中的字段是找到的相应第一个文件(不会被删除的文件)。每次在该关联数组中找到一个md5sum时,该文件将被删除,并且将创建一个到相应键的相应链接(在检查到要删除的文件不是原始文件后)。将目录作为参数进行搜索;没有参数在当前目录内执行搜索。

#!/bin/bash 

shopt -s globstar nullglob 

(($#==0)) && set . 

declare -A md5sum=() || exit 1; 
while(($#)); do 
    [[ $1 ]] || continue 
    for file in "$1"/**/*; do 
     [[ -f $file ]] || continue 
     h=$(md5sum < "$file") || continue 
     read h _ <<< "$h" # This line is optional: to remove the hyphen in the md5sm 
     if [[ ${md5sum[$h]} ]]; then 
      # already seen this md5sum 
      [[ "$file" -ef "${md5sum[$h]}" ]] && continue # prevent unwanted removal! 
      rm -- "$file" || continue 
      ln -rs -- "${md5sum[$h]}" "$file" 
     else 
      # first time seeing this file 
      md5sum[$h]=$file 
     fi 
    done 
    shift 
done 

(未经测试,使用你自己的风险!)

+0

它正确保存为ln -r。-r开关不存在。 – Alexander 2015-02-11 14:07:34

+0

@Alexander:这可能取决于您正在使用的“ln”版本。很高兴帮助! – 2015-02-11 19:07:19