我有两个文件夹,其中包含一些常用文件,我想从
xyz
文件夹中删除重复的文件。
文件夹1:
/abc/file1.csv
/abc/file2.csv
/abc/file3.csv
/abc/file4.csv
文件夹2:
/xyz/file1.csv
/xyz/file5.csv
我想比较两个文件夹并从
/xyz
文件夹中删除重复项。输出应该是:file5.csv
目前我正在使用:
find "/xyz" "/abc" "/abc" -printf '%P\n' | sort | uniq -u | -exec rm {} \;
但是它失败了:如果
-exec
不是拼写错误,您可以运行以下命令来查找包含二进制文件的包:
command-not-found -exec
-bash: -exec: command not found
-exec
是 find
的一个选项,当您启动管道时,您已经退出了命令 find
。
xargs
,它会从 stdin 获取所有数据并附加到程序中。
未经测试
find "/xyz" "/abc" "/abc" -printf '%P\n' | sort | uniq -u | xargs rm
查找 234 和 123 目录中的每个文件,通过 -printf 获取文件名,对它们进行排序,uniq -d 给出重复项列表,通过 sed 返回路径,使用 123 目录删除重复项,并将文件传递给 xargs rm
命令:
find ./234 ./123 -type f -printf '%P\n' | sort | uniq -d | sed 's/^/.\/123\//g' | xargs rm
如果您位于 ./123 目录中并在 find 中使用文件夹的完整路径,则不需要 sed。
另一种方法:只需找到 abc 中的文件并尝试从 xyz 中删除它们:
未经测试
find /abc -type f -printf 'rm -f /xyz/%P' | sh
FileList=$(ls)
for D1 in $FileList ;do
if [[ -f $D1 ]]; then
for D2 in $FileList ;do
if [[ -f $D2 ]]; then
if [[ $D1 == $D2 ]]; then
: 'Skip Orignal File'
else
if [[ $(md5sum $D1 | cut -d'=' -f 2 | cut -d ' ' -f 1 ) == $(md5sum $D2 | cut -d'=' -f 2 | cut -d ' ' -f 1 ) ]]; then
echo "Duplicate File Found : $D2"
rm -rf $D2
fi #Detect Duplicate Using MD5
fi #Skip Orginal File
fi #D2 File available Then Next
done
fi #D1 File available Then Next
done