0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Rename Multiple Files Easily

Use a Bash loop like for file in *.txt; do mv "$file" "new_$file"; done to rename multiple files by adding a prefix or changing names.
📋

Examples

InputFiles: report1.txt, report2.txt
OutputRenamed to: new_report1.txt, new_report2.txt
InputFiles: image1.jpg, image2.jpg
OutputRenamed to: photo1.jpg, photo2.jpg (using mv "$file" "photo${file#image}" )
InputFiles: fileA, fileB (no extension)
OutputRenamed to: fileA_backup, fileB_backup (using mv "$file" "${file}_backup" )
🧠

How to Think About It

To rename multiple files, think about how to select the files you want, then decide the new names based on a pattern. Use a loop to go through each file and rename it with the mv command. This way, you automate renaming many files quickly.
📐

Algorithm

1
Get the list of files matching a pattern (e.g., *.txt).
2
For each file in the list, create a new name based on your rule (like adding a prefix).
3
Use the move command to rename the file to the new name.
4
Repeat until all files are renamed.
💻

Code

bash
for file in *.txt; do
  new_name="new_$file"
  echo "Renaming $file to $new_name"
  mv "$file" "$new_name"
done
Output
Renaming report1.txt to new_report1.txt Renaming report2.txt to new_report2.txt
🔍

Dry Run

Let's trace renaming report1.txt and report2.txt by adding 'new_' prefix.

1

List files

Files found: report1.txt, report2.txt

2

Rename first file

file=report1.txt, new_name=new_report1.txt, command: mv "report1.txt" "new_report1.txt"

3

Rename second file

file=report2.txt, new_name=new_report2.txt, command: mv "report2.txt" "new_report2.txt"

FileNew NameCommand
report1.txtnew_report1.txtmv "report1.txt" "new_report1.txt"
report2.txtnew_report2.txtmv "report2.txt" "new_report2.txt"
💡

Why This Works

Step 1: Loop over files

The for loop picks each file matching the pattern one by one.

Step 2: Create new name

We build the new file name by adding a prefix like new_ before the original name.

Step 3: Rename file

The mv command renames the file from the old name to the new name.

🔄

Alternative Approaches

Using rename command
bash
rename 's/^/new_/' *.txt
This is shorter but depends on the availability of the rename utility and its syntax varies by system.
Using find with exec
bash
find . -maxdepth 1 -name '*.txt' -exec bash -c 'mv "$0" "new_$0"' {} \;
Useful for more complex file selection but more complex syntax.
Using a numbered sequence
bash
count=1
for file in *.txt; do
  mv "$file" "file_$count.txt"
  ((count++))
done
Renames files with a sequence number instead of prefixing original names.

Complexity: O(n) time, O(1) space

Time Complexity

The script processes each file once in a loop, so time grows linearly with the number of files.

Space Complexity

The script uses constant extra space, only storing temporary variables for each file.

Which Approach is Fastest?

Using the rename command is fastest for simple patterns, but loops offer more flexibility.

ApproachTimeSpaceBest For
Bash loop with mvO(n)O(1)Flexible renaming rules
rename commandO(n)O(1)Simple pattern renaming, faster
find with execO(n)O(1)Complex file selection
💡
Always test your rename script with echo before running mv to avoid mistakes.
⚠️
Forgetting to quote file names with spaces, causing errors or wrong renames.