我有树文件夹folder-1,folder-2和folder-3都位于我的/home/中。
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相同的内容。
/home/folder-2/x.txt没有与/home/folder-1/x.txt文件夹相同的内容。
我知道如果我想比较不同文件夹中两个文件的“内容”(而不是大小或名称),我可以使用:
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来编写这个文件,就好像folder-2和folder-3上有一个文件“x.txt”的内容与x.txt相同,然后我将发出一个新的命令、操作或决定。
但到目前为止,我还没有发现任何能具体帮助我的东西。
注意:folder-2和folder-3没有子目录。
发布于 2021-04-29 23:13:34
使用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: %s\n' "$file2"
dupfound=true
fi
done
if [ -z "$dupfound" ]
then "No duplicate found"
fihttps://stackoverflow.com/questions/67325683
复制相似问题