检查特定目录下的x.txt文件内容是否与其他目录下的x.txt文件内容相同,bash有条件
Check if the contents of the x.txt file under a particular directory is the same as the x.txt file content in other directories, bash conditional
我的 /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
内容相同。
/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
没有子目录。
使用 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"
fi
我的 /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
内容相同。
/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
没有子目录。
使用 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"
fi