BASH

Batch Rename Files Using Bash Loops and Patterns

Efficiently rename multiple files in a directory using bash scripting. This snippet renames files by adding a prefix or replacing parts of their names.

#!/bin/bash

# Directory to perform renaming (current directory if empty)
TARGET_DIR="${1:-.}"

# Check if target directory exists
if [ ! -d "$TARGET_DIR" ]; then
    echo "Error: Directory '$TARGET_DIR' not found."
    exit 1
fi

echo "Renaming files in: $TARGET_DIR"

# --- Example 1: Add a prefix to all .jpg files ---
# PREVIEW ONLY:
echo -e "
--- Preview: Adding 'resized_' prefix to .jpg files ---"
for file in "$TARGET_DIR"/*.jpg; do
    if [ -f "$file" ]; then # Ensure it's a file, not a glob that didn't match
        filename=$(basename -- "$file")
        dirname=$(dirname -- "$file")
        new_filename="resized_$filename"
        echo "Would rename: '$file' to '$dirname/$new_filename'"
    fi
done

# UNCOMMENT THE FOLLOWING BLOCK TO ACTUALLY PERFORM THE RENAMING
# read -p "Perform renaming (add 'resized_' prefix to .jpg)? (y/N): " -n 1 -r
# echo
# if [[ $REPLY =~ ^[Yy]$ ]]; then
#     for file in "$TARGET_DIR"/*.jpg; do
#         if [ -f "$file" ]; then
#             filename=$(basename -- "$file")
#             dirname=$(dirname -- "$file")
#             new_filename="resized_$filename"
#             mv "$file" "$dirname/$new_filename"
#             echo "Renamed: '$filename' to '$new_filename'"
#         fi
#     done
# else
#     echo "Skipping .jpg renaming."
# fi


# --- Example 2: Replace spaces with underscores in all .png files ---
# PREVIEW ONLY:
echo -e "
--- Preview: Replacing spaces with underscores in .png files ---"
for file in "$TARGET_DIR"/*.png; do
    if [ -f "$file" ]; then
        filename=$(basename -- "$file")
        dirname=$(dirname -- "$file")
        new_filename="${filename// /_}" # Replace all spaces with underscores
        if [ "$filename" != "$new_filename" ]; then # Only show if a change would occur
            echo "Would rename: '$file' to '$dirname/$new_filename'"
        fi
    fi
done

# UNCOMMENT THE FOLLOWING BLOCK TO ACTUALLY PERFORM THE RENAMING
# read -p "Perform renaming (replace spaces in .png)? (y/N): " -n 1 -r
# echo
# if [[ $REPLY =~ ^[Yy]$ ]]; then
#     for file in "$TARGET_DIR"/*.png; do
#         if [ -f "$file" ]; then
#             filename=$(basename -- "$file")
#             dirname=$(dirname -- "$file")
#             new_filename="${filename// /_}"
#             if [ "$filename" != "$new_filename" ]; then
#                 mv "$file" "$dirname/$new_filename"
#                 echo "Renamed: '$filename' to '$new_filename'"
#             fi
#         fi
#     done
# else
#     echo "Skipping .png renaming."
# fi

echo -e "
Batch renaming script finished."

# To test:
# mkdir -p test_files
# touch "test_files/image1.jpg" "test_files/image_2.jpg" "test_files/my photo.png" "test_files/another pic.png"
# chmod +x batch_rename.sh
# ./batch_rename.sh test_files
# rm -rf test_files # Cleanup
How it works: This bash script provides a powerful way to perform batch file renaming based on patterns within a specified directory. It uses a `for` loop to iterate through files matching a glob pattern and demonstrates two common renaming operations: adding a prefix and replacing characters (e.g., spaces with underscores). The script includes a preview mode and user confirmation before actual renaming, making it safe for managing large sets of web assets like images, CSS, or JavaScript files.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs