[Bulk] Rename Files in Linux – The Ultimate Guide

If you’ve ever configured Set a New PATH in csh and tcsh, explored the power of the LS command, or processed RAW images with RawTherapee, you already understand that working efficiently in Linux often means mastering the terminal. And one of the most essential — yet underestimated — skills is renaming files.

At first, renaming a file in Linux seems trivial. But once you manage servers, large photo libraries, development projects, or automated backups, renaming becomes a serious task. You may need to rename thousands of files, replace patterns recursively, normalize case, or standardize naming conventions.

This guide walks you step by step — from basic commands to advanced, real-world techniques — structured clearly into basic and advanced methods. It’s written from practical experience, not just theory.

Let’s start with the fundamentals.


Basic Commands for Renaming Files

Renaming a Single File with mv

In Linux, the primary command for renaming files is mv. Although it stands for “move,” renaming a file in the same directory is technically just moving it to a new name.

Basic Syntax

mv old_filename new_filename

Example

mv notes.txt notes-2026.txt

The file stays in the same directory but gets a new name.

Using Full Paths

You can also rename files by specifying paths:

mv /home/user/documents/old.txt /home/user/documents/new.txt

Avoid Overwriting Existing Files

By default, mv will overwrite existing files. To prevent accidental data loss:

mv -i file.txt existing.txt

The -i option prompts for confirmation.

Force Overwrite

mv -f file.txt existing.txt

Use this carefully, especially on production systems.


Handling Filenames with Spaces

If a filename contains spaces, wrap it in quotes:

mv "my file.txt" "my_file.txt"

Without quotes, the shell will interpret each word separately.


Using Wildcards Carefully

Shell wildcards can help select multiple files:

ls *.txt

Always inspect matches first using ls before running bulk commands. This simple habit can prevent major mistakes.


Advanced Commands for Batch Renaming

Once you start working with large datasets or nested directories, basic mv is no longer sufficient. That’s where rename and find become extremely powerful.


The rename Command (Bulk Renaming Made Easy)

The rename command is designed specifically for batch renaming. Most modern Linux distributions use the Perl-based version, which supports regular expressions.

Basic Syntax

rename 's/old/new/' files

It performs a substitution using regex syntax.


Replacing a String in Multiple Files

rename 's/draft/final/' *.txt

All .txt files containing “draft” in their names will have it replaced with “final”.


Removing a Pattern

rename 's/_backup//' *.sql

Removes _backup from SQL dump filenames.


Changing File Extensions

rename 's/\.jpeg$/\.jpg/' *.jpeg

Converts .jpeg files to .jpg.


Always Use Dry Run First

Before applying mass changes:

rename -n 's/old/new/' *.txt

The -n flag shows what would happen without making changes. I strongly recommend making this a habit.


Replacing Spaces with Underscores

rename 's/ /_/' *

This is useful for normalizing filenames for scripts and servers.


Converting Filenames to Lowercase

rename 'y/A-Z/a-z/' *

This is particularly helpful when migrating files from Windows environments or preparing web content for case-sensitive systems.


Recursive Renaming with find and mv

When you need to rename files across subdirectories, find becomes essential.

find allows you to locate files based on conditions and execute commands on each result.


Append a Suffix to All .log Files Recursively

find . -type f -name "*.log" -exec mv {} {}.bak \;

This adds .bak to every .log file under the current directory.


Replace a String Recursively Using Bash Substitution

For more advanced operations:

find . -type f -name "*draft*" -exec bash -c 'mv "$0" "${0//draft/final}"' {} \;

This replaces “draft” with “final” in filenames throughout all subdirectories.

It’s powerful because it uses Bash string substitution, allowing flexible transformations.


Using -depth for Safer Renaming

When renaming directories and files together:

find . -depth -exec rename 's/old/new/' {} +

The -depth option ensures files are processed before their parent directories, avoiding path conflicts.


Real-World Example: Cleaning Up a Media Library

Imagine exporting images from your camera and editing them in RawTherapee. You might get filenames like:

IMG_1001.RW2
IMG_1002.RW2

To standardize them:

rename 's/IMG_/2026_trip_/' *.RW2

Or recursively:

find . -type f -name "*.RW2" -exec rename 's/IMG_/2026_trip_/' {} +

For large collections, this can save hours of manual work.


Common Issues and Troubleshooting

“rename: command not found”

Some systems don’t include it by default.

On Debian/Ubuntu:

sudo apt install rename

On RHEL/CentOS:

sudo dnf install prename

Different rename Versions

Some distributions include a simpler version where syntax looks like:

rename old new files

Check which version you have:

rename --version

Understanding your environment prevents syntax errors.


Best Practices for Safe Bulk Renaming

From experience, here are habits that make a big difference:

  • Always preview with ls or use dry-run mode.
  • Avoid running massive rename operations as root unless absolutely necessary.
  • Test commands in a small directory first.
  • Use quotes around variables in scripts.
  • Consider using version control (git mv) when working inside repositories.

Bulk renaming is powerful — but power requires discipline.


Conclusion

Renaming files in Linux starts with a simple mv, but it scales into something much more powerful once you combine rename, regular expressions, and find.

Whether you’re organizing server logs, restructuring a project, or cleaning up thousands of photos, mastering these techniques transforms how you work with Linux. It reduces manual effort, minimizes errors, and gives you full control over your filesystem.

The more you practice these commands, the more natural they become — and soon, renaming thousands of files safely will feel as simple as renaming one.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *