Bulk File Rename

Bulk renames files with pattern matching and automatic numbering features.

Published: May 01, 2024

Detailed Information

This script bulk renames files in a directory. Simplifies file management with pattern matching and automatic numbering features.

What Does This Script Do?

This script automates file renaming:

  • Finds files matching a pattern
  • Renames files with prefix and number
  • Preserves file extensions
  • Performs safe renaming

Why Should You Use It?

Bulk file renaming simplifies file management:

  • Time Savings: Rename hundreds of files with single command
  • Organization: Organize files with consistent naming
  • Automation: Automate manual processes

How to Use

Step-by-Step Usage Guide

1. Create Script File

nano bulk_rename.sh

2. Make Executable

chmod +x bulk_rename.sh

3. Run Script

./bulk_rename.sh /path/to/files "*.txt" "document_"

4. Examples

# Rename all .jpg files to image_001.jpg, image_002.jpg
./bulk_rename.sh /photos "*.jpg" "image_"

# Rename all .pdf files to doc_001.pdf
./bulk_rename.sh /documents "*.pdf" "doc_"

Requirements

Requirements

  • Bash: Bash shell
  • File Access: Write permission to directory and files

Use Cases

Use Cases

1. Photo Organization

Organize photos with consistent naming.

2. Document Management

Organize documents with numbered format.

Examples

Usage Examples

Example 1: Photo Renaming

./bulk_rename.sh /home/user/photos "*.jpg" "vacation_"

Code

#!/bin/bash

# Bulk File Rename Script

if [ $# -lt 2 ]; then
    echo "Usage: $0 <directory> <pattern> [prefix]"
    echo "Example: $0 /path/to/files \"*.txt\" \"document_\""
    exit 1
fi

DIR="$1"
PATTERN="$2"
PREFIX="${3:-file_}"

if [ ! -d "$DIR" ]; then
    echo "Error: Directory not found: $DIR"
    exit 1
fi

cd "$DIR" || exit 1

COUNT=1
for file in $PATTERN; do
    if [ -f "$file" ]; then
        EXT="${file##*.}"
        NEW_NAME="${PREFIX}$(printf "%03d" $COUNT).${EXT}"
        mv "$file" "$NEW_NAME"
        echo "Renamed: $file -> $NEW_NAME"
        ((COUNT++))
    fi
done

echo "Renaming completed!"

Usage

chmod +x bulk_rename.sh
./bulk_rename.sh /path/to/files "*.txt" "document_"

Troubleshooting

Troubleshooting

Problem: "Permission denied"

Solution: Give write permission to directory:

chmod +w /path/to/directory

Tags

file rename bulk rename file management organize