How to Safely Rename Files in Bulk Using the Rename Command in Linux

If you need to rename a single file in Linux, the mv (move) command works perfectly. However, if you need to rename 500 image files—for example, changing all .jpeg extensions to .jpg, or removing the word “draft” from 100 document titles—using mv manually is impossible. While you could write a complex bash script with a for loop, there is a much faster, dedicated tool for this job: the rename command.

The rename command utilizes Perl-compatible regular expressions (Regex) to search for specific text patterns within filenames and replace them instantly across an entire directory.

Installing the Rename Utility

Depending on your Linux distribution, the Perl-based rename command might not be installed by default. (Some distributions include an older, less powerful version from the util-linux package).

To ensure you have the correct, powerful Perl version, run the following installation command:

  • Ubuntu/Debian: sudo apt install rename
  • CentOS/RHEL: sudo yum install prename

The Basic Syntax of Rename

If you have ever used the sed command, the syntax for rename will look very familiar. It follows a strict substitute structure:

rename 's/search_pattern/replacement_text/' files_to_change

  • s: Stands for “substitute” (find and replace).
  • / / /: The forward slashes act as dividers.
  • files_to_change: Usually a wildcard (like *.txt) telling the command which files to target.

Practical Examples of Bulk Renaming

1. Changing File Extensions

You have a folder full of images ending in .jpeg, and a web application strictly requires them to end in .jpg.

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

(Note: The backslash before the dot escapes it, ensuring Regex treats it as a literal period. The dollar sign ‘$’ ensures it only changes “.jpeg” if it appears at the very end of the filename).

2. Removing a Word from Filenames

You have dozens of files named report_draft_2023.pdf, budget_draft_2023.pdf, etc. You want to delete the word “draft_” from all of them.

rename 's/draft_//' *.pdf

By leaving the replacement section completely empty between the slashes, the command simply deletes the searched word.

3. Converting Uppercase to Lowercase

Linux is strictly case-sensitive. If a client sends you a folder of files named IMAGE_001.JPG, and your web server expects lowercase, you can use the translation (y) feature of rename instead of substitution (s).

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

The Golden Rule: Always Use the “Dry Run” Flag (-n)

Bulk renaming files is dangerous. If you write a flawed Regex pattern, you could accidentally rename hundreds of critical system files into gibberish, permanently ruining your operating system.

To prevent this, always use the -n (no action) flag before running a real command. This tells the utility to perform a “dry run.”

rename -n 's/draft_//' *.pdf

When you press Enter, the terminal will print out a preview of exactly what would happen (e.g., rename(report_draft.pdf, report.pdf)), but it will not actually change a single file on your hard drive.

Review the printed list carefully. If the preview looks exactly as you intended, press the Up arrow on your keyboard, delete the -n flag, and press Enter to execute the command for real.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.