Reputation: 95
I have a question. It's a part of my homework and i really dont know how to do it. I have an archive that contains the tree of directories and a file called sumy.md5. I have to search every file in this tree and check if their checksums are also in sumy.md5 file. If they are i have to move them to another directory. I would be very grateful if someone could tell me how to go through the whole directory and check if the checksum of a file is also in file sumy.md5
The code I have tried so far.
for f in (find ./AA/* -type f )
while read -r file;
do
b=$(md5sum $file | cut -d' ' -f1)
if [ $a == $b ] then
echo "Found It"
else echo "File not found"
fi
done < sumy.md5
Upvotes: 0
Views: 2015
Reputation: 4939
There are some problems with your script. Copying your comment and formatting:
# the following line has a syntax error (missing "$")
# but it's also not used in the loop
for f in (find ./AA/* -type f )
# syntax error here trying to jump into the while loop
while read -r file; do
b=$(md5sum $file | cut -d' ' -f1)
# the variable "a" is never defined
if [ $a == $b ]
then
echo "Found It"
else
echo "File not found"
fi
done < sumy.md5
The above seems to iterate over the files in the directory (incorrectly), then iterate over every line in the sumy.md5 file. If the md5 contents are output from md5sum
then you could probably just use the --check
option, but if not, it's easier just to search the contents using something like grep.
for file in $( find ./AA/* -type f )
do
b=$(md5sum $file | cut -d' ' -f1)
# "q" just exits with status code, 0 or 1 on found or not
# "i" says to ignore case
if grep -qi "$b" "sumy.md5"
then
echo "Found It"
else
echo "File not found"
fi
done
If found, use something like cp --parents
or maybe rsync
to copy the file to whatever directory, and that will also create the required directory structure without having to deal with it manually.
Upvotes: 1