This guide provides instructions on how to create a Bash script to rename multiple files. This script also allows users to provide custom settings and options.
Overview
The script allows you to rename files in a directory based on your specified pattern. It’s highly configurable and includes error checking to prevent common mistakes.
Prerequisites
This guide assumes you have the following:
- A Linux operating system with a Bash shell environment.
- Basic knowledge of Bash scripting, such as creating, editing, and executing scripts.
- Basic knowledge of the
mv
command, used to rename files.
Script Structure
Let’s break down the script into various sections:
- Shebang: The shebang
#!/bin/bash
at the beginning of the script informs the system that the script is to be executed by the Bash shell. - Variable Declaration: Here we declare variables that are used in the script.
- Usage Function: This function provides information on how to use the script.
- Error Handling: This section checks for any potential errors that may occur during script execution.
- Main Logic: The main logic of the script where the renaming of files happens.
Creating the Bash Script
Below is the entire bash script with comments explaining each section:
#!/bin/bash
# Variable Declaration
DIR="" # Directory to perform renaming
PATTERN="" # Pattern of files to match
PREFIX="" # Prefix to add to the files
SUFFIX="" # Suffix to add to the files
DRY_RUN=0 # Dry run flag
# Usage Function
usage() {
echo "Usage: $0 [-d directory] [-p pattern] [-P prefix] [-s suffix] [-n]"
echo
echo "Options:"
echo " -d directory : Directory to perform renaming"
echo " -p pattern : Pattern of files to match"
echo " -P prefix : Prefix to add to the files"
echo " -s suffix : Suffix to add to the files"
echo " -n : Perform a dry run without executing"
exit 1
}
# Parse Command Line Arguments
while getopts "d:p:P:s:n" OPTION; do
case $OPTION in
d)
DIR=$OPTARG
;;
p)
PATTERN=$OPTARG
;;
P)
PREFIX=$OPTARG
;;
s)
SUFFIX=$OPTARG
;;
n)
DRY_RUN=1
;;
?)
usage
;;
esac
done
# Error Checking
if [[ -z "$DIR" || -z "$PATTERN" ]]; then
echo "ERROR: Directory and pattern are mandatory."
usage
fi
if [ ! -d "$DIR" ]; then
echo "ERROR: Directory $DIR does not exist."
exit 1
fi
# Main Logic
for FILE in "$DIR/$PATTERN"; do
NEW_FILE="${DIR}/${PREFIX}${FILE##*/}${SUFFIX}"
echo "Renaming $FILE to $NEW_FILE"
if [ $DRY_RUN -eq 0 ]; then
mv "$FILE" "$NEW_FILE"
fi
done
Using the Script
To use this script, follow these steps:
- Save the script in a file, such as
rename_files.sh
. - Make the script executable by running
chmod +x rename_files.sh
. - Run the script with your desired options. For example, to add a prefix
new_
to all.txt
files in the/tmp
directory, run:
./rename_files.sh -d /tmp -p '*.txt' -P new_
To perform a dry run without actually renaming files, use the -n
option:
./rename_files.sh -d /tmp -p '*.txt' -P new_ -n
This script is flexible and customizable, meeting a variety of file renaming needs. If you encounter any issues, refer back to this documentation to understand the functionality and usage of the script.