find "$source_directory" -type f ( -name '*.jpeg' -o -name '*.jpg' -o -name '*.JPEG' -o
-name '*.JPG' -o -name '*.png' -o -name '*.PNG' ) -print | while read -r file; do
# Dosyanın bulunduğu en üst dizini al
top_level_directory=$(basename "$(dirname "$file")")
# Dizin adındaki " ", ".", "-", "#" gibi karakterleri "_" ile değiştir
sanitized_directory_name=$(echo "$top_level_directory" | sed 's/[ .-#?,+&=]/_/g')
# Dosya isminin başına sanitized_directory_name_ ekle
new_filename="${sanitized_directory_name}_$(basename "$file")"
# Dosyayı yeni isimle hedef dizine taşı
mv "$file" "${destination_directory}/${new_filename}"
done
Hi, I take “invalid expression; I was expecting to find a ‘)’ somewhere but did not see one.” How can I handle with error please help me.
5
It looks like you’re missing a closing parenthesis in your find command. That’s why you’re getting the error. Here’s how your script should look:
find "$source_directory" -type f ( -name '*.jpeg' -o -name '*.jpg' -o -name '*.JPEG' -o -name '*.JPG' -o -name '*.png' -o -name '*.PNG' ) -print | while read -r file; do
# Get the top-level directory where the file is located
top_level_directory=$(basename "$(dirname "$file")")
# Replace characters like " ", ".", "-", "#" in the directory name with "_"
sanitized_directory_name=$(echo "$top_level_directory" | sed 's/[ .-#?,+&=]/_/g')
# Add sanitized directory name as a prefix to the filename
new_filename="${sanitized_directory_name}_$(basename "$file")"
# Move the file to the destination directory with the new name
mv "$file" "${destination_directory}/${new_filename}"
done
Make sure you’ve set your source_directory and destination_directory variables properly. This script should run without any issues now.
Louis Cha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2