Batch rename files using the parent directory name
After coming across suitable images, I save them immediately. However, over time, the directories and files of images sourced from the internet became extremely cluttered. I started reclassifying and organizing them, moving images with similar attributes to appropriate directories. Ultimately, while the images were neatly categorized, their arbitrary filenames were irksome. Facing this issue, I consulted Google and eventually stumbled upon an excellent solution: Batch rename files using the parent directory name.
Open the
BATCH RENAME FILES USING THE PARENT DIRECTORY NAME[/caption]
Note:
Terminal app, then enter the following bash script code and press Enter to run:
for dir in */; do
count=1
for file in "${dir}"*; do
if [[ -f "$file" ]]; then
base=$(basename "$file")
ext="${base##*.}"
newname="${dir%/}_${count}.${ext}"
mv "$file" "${dir}${newname}"
((count++))
fi
done
done
This script will iterate through each subdirectory and rename the image files inside to the name of the parent directory followed by an incremental number while retaining the original file extension.
- Please use the
cdcommand in the Terminal app to navigate to the top directory containing the images you want to rename. - Enter the bash script and press the
Enterkey to execute the command.
BATCH RENAME FILES USING THE PARENT DIRECTORY NAME[/caption]
Note:
- Please make sure to backup your data as a precaution.
- The provided script is written based on your specific requirements. It assumes you are in the parent directory of these images and that each subdirectory contains the image files you wish to rename.
Batch command to Generate Text Files Based on Lines from a Source File Batch command to extract the filenames in the current directory Batch command to create blank text files Batch rename files using the parent directory name
Comments
Post a Comment