我的/home/
中都有树文件夹folder-1
、folder-2
和folder-3
。
folder-1
包含x.txt
,但folder-2
包含文件x.txt
、y.txt
、z.txt
等。
folder-3
包含文件x.txt
、y.txt
、z.txt
等
只有/home/folder-2/x.txt
具有与/home/folder-1/x.txt
相同的内容。
CCD_ 17不具有与文件夹CCD_ 18相同的内容。
我知道如果我想比较";内容";(而不是大小或名称(在不同的文件夹中的两个文件,我可以使用:
a="/home/folder-1/x.txt"
b="/home/folder-2/x.txt"
if cmp -s "$a" "$b"; then
printf 'A file with duplicate content found'
else
printf 'No file with duplicate content found'
fi
但当我有一个文件和你的内容与其他文件夹中有很多文件的文件进行比较时,这并不是一件实用和高效的事情。
我研究了一种方法来调整我的代码,编写另一个条件bash,这样我就可以将/home/folder-1/x.txt
与/home/folder-2/{all files}
和/home/folder-3/{all files}
中的所有现有文件进行比较。
我必须使用条件bash来写这篇文章,就好像有一个文件";x.txt";在具有与x.txt
相同内容的folder-2
和folder-3
上,则我将拍摄新的命令、动作或决策。
但到目前为止,我还没有找到任何能具体帮助我的东西。
注意:folder-2
和folder-3
没有子目录。
使用for
循环。
file1=/home/folder-1/x.txt
dupfound=
for file2 in /home/folder-2/* /home/folder-3/*
do
if cmp -s "$file1" "$file2"
then
printf 'Duplicate found: %sn' "$file2"
dupfound=true
fi
done
if [ -z "$dupfound" ]
then "No duplicate found"
fi