检查特定目录下x.txt文件的内容是否与其他目录下的x.txt文件内容相同,bash条件



我的/home/中都有树文件夹folder-1folder-2folder-3

folder-1包含x.txt,但folder-2包含文件x.txty.txtz.txt等。

folder-3包含文件x.txty.txtz.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-2folder-3上,则我将拍摄新的命令、动作或决策。

但到目前为止,我还没有找到任何能具体帮助我的东西。

注意:folder-2folder-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

最新更新