I am trying to automate the hashing of certain files (popular image and video formats) by using their file extension (not perfect but will suffice for my needs) recursively from the directory the script is run in.
I have little experience with bash and cannot figure out why this loop does not behave as intended although my suspicions are with the assignment of the F_EXTENSION
variable and the |
to tr
command. It will hash all the files in the directory regardless of their extension. I am clearly doing something wrong.
The following is just a small part of the code for clarity, many more variables are assigned elsewhere (such as the filenames referenced below).
FILES=$( find ./* -type f )
EXTENSIONS=(.jpg .gif .png .bmp .avi .mpg .mov .mkv .flv .wmv .mp4)
SAVEIFS=$IFS
IFS=$'\n'
for FILE in $FILES; do
F_EXTENSION=${FILE: -4} | tr '[:upper:]' '[:lower:]'
if [[ "${EXTENSIONS[@]}" =~ "${F_EXTENSION}" ]]; then
HASH=$( md5sum $FILE )
echo ${HASH} | cut -c1-32 >> ${TEMPFILE}
else
echo "Skipping ${FILE}" >> ${LOGFILE}
fi
done
IFS=$SAVEIFS
After trying various methods I have to concede its time for a fresh set of eyes. Any thoughts?