Automate Repetitive File Renaming Tasks with Bash Scripts

Automate Repetitive File Renaming Tasks with Bash Scripts

Automate Repetitive File Renaming Tasks with Bash Scripts

 

Renaming multiple files manually can be time-consuming, repetitive, and error-prone—especially when working with logs, image files, or downloads that follow inconsistent naming schemes. Fortunately, Bash scripts offer a powerful, fast, and flexible solution to automate file renaming tasks at scale.

In this blog post, we’ll walk through how to write reusable Bash scripts that rename files based on dates, extensions, numeric sequences, and other patterns. By the end, you’ll have a toolkit of useful scripting techniques for organizing your files efficiently.

1. Getting Started: Why Automate File Renaming?

Before jumping into code, let’s look at a few real-world use cases where automated renaming proves useful:

  • Photo organization: Rename hundreds of photos from IMG_1998.JPG to a structured format like vacation_1998-07-12.jpg.
  • Log file rotation: Rename server logs from log.txt to log_2024-06-01.txt daily.
  • Extension normalization: Rename .jpeg files to .jpg for consistency.

Bash can automate all these tasks with simple scripts, saving hours of manual effort.

2. Renaming Files by Extension

Let’s start with a classic task: converting files with the .jpeg extension to .jpg.

#!/bin/bash
for file in *.jpeg; do
  new_name="${file%.jpeg}.jpg"
  mv "$file" "$new_name"
done

Explanation:

  • *.jpeg targets only files with the .jpeg extension.
  • ${file%.jpeg} removes the .jpeg suffix from filenames.
  • mv performs the file rename.

Performance tip: For directories with thousands of files, consider wrapping your script in find or breaking logic into functions to reduce memory usage.

3. Appending Dates to Filenames

Want to timestamp all your log files with the current date? This is a great way to keep historical logs.

#!/bin/bash
DATE=$(date +%F)
for file in *.log; do
  new_name="${file%.log}_$DATE.log"
  mv "$file" "$new_name"
done

Key concept: date +%F returns the date in YYYY-MM-DD format, making it perfect for chronological sorting.

Tip: Use basename and dirname if your script works with nested directories.

4. Sequential Numbering of Files

Renaming images like photo1.jpg, photo2.jpg... is useful when organizing camera rolls or PDFs. Here’s how to create a numeric sequence:

#!/bin/bash
counter=1
for file in *.png; do
  new_name=$(printf "image_%03d.png" "$counter")
  mv "$file" "$new_name"
  ((counter++))
done

Explanation:

  • counter tracks numbering.
  • printf pads numbers with zeros (e.g., 001, 002…).

Real-world use: Reinforces consistent naming conventions for batch uploading or processing pipelines.

5. Building a Reusable File Renaming Script

Let’s create a general-purpose renamer that takes a pattern and applies it dynamically to files:

#!/bin/bash
usage() {
  echo "Usage: $0 [pattern] [extension]"
  echo "Example: $0 vacation_ .jpg"
  exit 1
}

[ $# -ne 2 ] && usage

PATTERN=$1
EXT=$2

counter=1
for file in *$EXT; do
  new_name=$(printf "%s%03d%s" "$PATTERN" "$counter" "$EXT")
  mv "$file" "$new_name"
  ((counter++))
done

You can run this script with parameters like:

./rename.sh log_ .txt

This will rename files to log_001.txt, log_002.txt, etc. Flexible and portable!

6. Safety First: Dry-Run Option

Never test a rename script on production data without a dry run. Add this preview mode:

#!/bin/bash
DRY_RUN=true
for file in *.txt; do
  new_name="processed_$file"
  if [ "$DRY_RUN" = true ]; then
    echo "Would rename $file to $new_name"
  else
    mv "$file" "$new_name"
  fi
done

Tip: Always version-control your scripts and add backups for sensitive file operations.

Conclusion

Whether you’re organizing photo albums, processing data logs, or preparing file batches for deployment, automated file renaming with Bash can save you significant time and protect your workflow from manual errors. With reusable patterns, dry-run safety checks, and timestamp options, you’re ready to process files like a pro.

Want to take it further? Try integrating your Bash scripts into cron jobs or systemd services for scheduled automation.

 

Useful links: