I have tree folders folder-1
,folder-2
and folder-3
located both in my /home/
.
folder-1
contains x.txt
, but folder-2
contains files x.txt
, y.txt
, z.txt
, etc.
folder-3
contains contains files x.txt
, y.txt
, z.txt
, etc.
Only /home/folder-2/x.txt
has the same content as /home/folder-1/x.txt
.
/home/folder-2/x.txt
do not have the same content that folder /home/folder-1/x.txt
.
I know if I want to compare "content" (and not the size or name) of two files in different folders I can use:
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
But this is not something practical and efficient when I have a single file and your content to compare with files from anothers folders that has many files.
I researched a way to adapt my code write another conditional bash so that I can compare /home/folder-1/x.txt
with all existing files in /home/folder-2/{all files}
and /home/folder-3/{all files}
.
I necessarily need to write this using conditional bash, as if there is a file "x.txt" on the folder-2
and folder-3
with the same content that x.txt
then I will shoot a new command, action or decision.
But so far I have not found anything that can help me concretely.
Note: folder-2
and folder-3
has no subdirectories.
Use a for
loop.
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