Hello everyone,
Hoping that this is a good place to post a question about Bash scripting. My wife and I have run into a problem in PhotoPrism where it keeps tagging pictures and videos with similar names together and so the thumbnail and the video do not match. I decided that rather than try to get her iPhone to tweak its naming it’s easier to just offload to a directory then rename every file to a UUID before sending to photoprism. I’m trying to write a bash script to simplify this but cannot get the internal loop to fire. The issue appears to be with the ‘while IFS= read -r -d ‘’ file; do’ portion. Is anyone able to spot what the issue may be?
#! /bin/bash
echo "This script will rename all files in this directory with unique names. Continue? (Y/N)"
read proceed
if [[ "$proceed" == "Y" ]]; then
echo "Proceed"
#use uuidgen -r to generate a random UUID.
#Currently appears to be skipping the loop entirely. the find command works so issue should be after the pipe.
# Troubleshooting
#Seems like changing IFS to $IFS helped. Now however it's also pulling others., don't think this is correct.
#verified that the find statement is correct, its the parsing afterwards that's wrong.
#tried removing the $'\0' after -d as that is string null in c. went to bash friendly '' based on https://stackoverflow.com/questions/57497365/what-does-the-bash-read-d-do
#issue definitely appears to be with the while statement
find ./ -type f \( -iname \*.jpg -o -iname \*.png \) | while IFS= read -r -d '' file; do
echo "in loop"
echo "$file"
#useful post https://itsfoss.gitlab.io/post/how-to-find-and-rename-files-in-linux/
#extract the directory and filename
dir=$(dirname "$file")
base=$(basename "$file")
echo "'$dir'/'$base'"
#use UUID's to get around photoprism poor handling of matching file names and apples high collision rate
new_name="$dir/$(uuidgen -r)"
echo "Renaming ${file} to ${new_name}"
#mv "$file" "$new_name" #uncomment to actually perform the rename.
done
echo "After loop"
else
echo "Cancelling"
fi


Interesting, the code shows up correctly for me in firefox. I wonder if that’s due to my instance?
This works perfectly! Thank you!!! In case anyone else finds themselves wondering about the ${0##*.} portion, I found this article to be very helpful. https://stackoverflow.com/questions/30980062/0-and-0-in-sh
edit: You beat me to it with your link on parameter expansion. I’ll be reading through that tonight as well. Thanks again.
If you want more help with Bash in the future, this is the best resource I’ve found in 13 years of writing bash professionally: https://mywiki.wooledge.org/EnglishFrontPage
Bash FAQs and pitfalls are the primary sections to look at there.
It might be instance related, I’m on PieFed, so perhaps the markdown implementation is different.
Also, I realized that the parameter expansion might not be straightforward and added the GNU docs on it, but looks like you found a post about it at the same time! Glad to hear it got you sorted out.