I have the following code which attempts to find all files which match certain patterns and copy them to a new directory, adding a suffix if the file already exists. This is because duplicate filenames appear in the original list - as they are found in different directories.
It works as expected except fot filenames containing spaces which the for in loop takes as delimiters.
This code is from various parts of stackoverflow, which I have used and I do not fully understand.
I have tried various combinations of quoting the filename, to the extent I only copied files with spaces.
Can someone advise as to how I can resolve this problem
patterns=( "wom*.gif" "StarB*.gif" "logos*.gif" )
for pattern in "${patterns[@]}"
do
for fname in $(find $source -name $pattern);
do
echo Path-and-Filename: $fname
filename=$(basename "$fname")
extension=${filename##*.}
filename=${filename%.*}
echo Just-Filename: $filename.$extension
suffix=""
count=0
while [ -f $dest$filename$suffix.$extension ]
do
count=$(($count+1))
suffix="("$count")"
done
cp -v -p "$fname" $dest"$filename"$suffix.$extension >>$dest"CopyImageFiles-"$(date +"%Y-%m-%d").log 2>>$dest"CopyImageFiles-"$(date +"%Y-%m-%d").log
done
done