摘要:文件夹 A 只有许多优秀文件 文件夹 B 有许多混合优秀/好/坏文件的文件夹 我如何才能删除文件夹 B 文件夹中的文件,以防它们位于文件夹 A 中。或者简单地放置相同的东西:如何制作检查文件夹 A 文件是否存在于文件夹 B 文件夹中,然后从文件夹 B 文件夹中删除?
解决方案的想法 也许是命令女巫:
导致复制程序不好:
无用的历史:文件是从文件夹 B 中的 Recuva 复制并部分排列,但其中很多是坏的,所以我第一次思考文件夹 B 是否存在与再次恢复的巫婆进行比较,但现在只是由 Recuva 在文件夹 A 中恢复的 Excelent 所以大多数 Excelent 会只是在文件夹 A 中。
示例文件树:
.
??? A
? ??? 1.png
? ??? 2.png
? ??? Excellent
? ??? e1.png
? ??? e2.png
??? B
??? 1.png
??? 2.png
??? Bad
? ??? 1.png
? ??? 2.png
? ??? e1.png
? ??? e2.png
??? Excellent
??? e1.png
??? e2.png
Run Code Online (Sandbox Code Playgroud)
以下是两种解决方案,具体取决于我们如何定义“重复”:
如果“重复”是指共享相同相对路径的两个文件,那么您可以使用find和xargs删除重复项。例如,假设你有
~/tmp% tree A
A
??? Excellent
??? bar
??? baz
??? foo
~/tmp% tree B
B
??? Bad
? ??? quux
??? Excellent
? ??? bar
? ??? baz
? ??? foo
??? Good
Run Code Online (Sandbox Code Playgroud)
然后
find /home/unutbu/tmp/A -depth -type f -print0 | xargs -0 -I{} bash -c 'rm "/home/unutbu/tmp/B${1#*A}"' - {}
Run Code Online (Sandbox Code Playgroud)
结果是
~/tmp% tree B
B
??? Bad
? ??? quux
??? Excellent
??? Good
Run Code Online (Sandbox Code Playgroud)
或者,如果“重复”是指两个文件共享相同的内容,尽管文件名可能不同,那么您可以使用rdfind:
sudo apt-get install rdfind
Run Code Online (Sandbox Code Playgroud)
如果我们有这个目录结构:
~/tmp% tree A
A
??? Excellent
??? bar
??? baz
??? foo
1 directory, 3 files
~/tmp% tree B
B
??? Bad
? ??? quux
??? Excellent
? ??? barbar
? ??? bazbaz
? ??? foofoo
??? Good
Run Code Online (Sandbox Code Playgroud)
where 与barbar具有相同的内容bar,对于bazbaz和也类似foofoo,那么
rdfind -deleteduplicates true A B
Run Code Online (Sandbox Code Playgroud)
结果是
~/tmp% tree B
B
??? Bad
? ??? quux
??? Excellent
??? Good
Run Code Online (Sandbox Code Playgroud)
替代解决方案,以防您的 Ubuntu 版本不包含 rdfind:
您可以改为使用fdupes:
sudo apt-get install fdupes
fdupes --recurse --delete --noprompt A B
Run Code Online (Sandbox Code Playgroud)