Table of contents
2. Give the script execution permission:
You can use the following script to batch rename picture files on a Mac:
1. Batch script
#!/bin/bash
# 设置文件夹路径和要替换的字符串及其替换内容
folder_path="/您的/文件夹/路径"
old_string="要替换的字符串"
new_string="替换后的字符串"
# 进入文件夹路径
cd "$folder_path"
# 遍历文件夹中的所有图片文件
for file in *.jpg *.jpeg *.png *.gif *.bmp *.tiff *.heic; do
if [[ -f "$file" ]]; then
# 获取文件名和扩展名
filename=$(basename "$file")
extension="${filename##*.}"
# 替换字符串并构建新文件名
new_name="${filename/$old_string/$new_string}"
# 重命名文件
mv "$file" "$new_name"
echo "已重命名 $file 为 $new_name"
fi
done
echo "批量替换完成!"
Save the above code as a text file, for example replace_pngName.sh
, then open a terminal and navigate to the folder where the script is located.
2. Give the script execution permission:
chmod +x replace_pngName.sh
3. Run the script:
./replace_pngName.sh
The script will traverse all image files in the specified folder, replace the specified string in the file name with a new string, and rename the file to the new name. Note that you can modify the folder paths, strings to replace and what to replace in the script as needed.
⚠️: Before running the script, be sure to back up your files in case something unexpected happens.