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

Efficient File Management from the Command Line

If you need to change the name of a single file in Linux, the standard mv (move) command works perfectly. However, if you need to rename hundreds of files at once—for example, changing the extension of 500 images from .jpeg to .jpg, or removing spaces from a directory full of document titles—using mv manually is incredibly tedious.

While you could write a complex bash loop to automate the process, Linux offers a much simpler solution: the rename command. This powerful utility uses Perl regular expressions to search through a list of files and modify their names based on patterns you define, allowing you to execute massive bulk operations in a fraction of a second.

Here is a complete guide on how to use the rename command to bulk rename files safely and efficiently.

Installing the Rename Command

While mv is built into every Linux distribution by default, rename is a separate utility that you may need to install. (Note: There are two different versions of the rename command in the Linux ecosystem. This guide focuses on the far more common and powerful Perl-based version, which is standard on Ubuntu and Debian-based systems).

To check if it is installed, open your terminal and type:

rename --version

If the terminal returns a “command not found” error, you can install it on Ubuntu/Debian using the apt package manager:

sudo apt update

sudo apt install rename

The Basic Syntax of the Rename Command

Because the command relies on Perl regular expressions, the syntax looks very similar to the sed command. The basic structure is:

rename 's/search_pattern/replacement_string/' files_to_rename

  • s: Stands for substitute.
  • search_pattern: The exact text you want to remove or change.
  • replacement_string: The new text you want to insert.
  • files_to_rename: The wildcard target (e.g., *.jpg to target all JPG files in the current directory).

Common Bulk Renaming Workflows

1. Changing File Extensions

If you have downloaded a folder full of HTML files but need them to be standard text files, you can quickly change the extension of every file in the directory.

rename 's/\.html$/.txt/' *.html

(Note: The backslash before the dot tells the system to treat it as a literal period, and the dollar sign ensures the command only replaces the text if it appears at the very end of the file name).

2. Removing Spaces from File Names

Spaces in file names can cause significant issues when writing scripts or moving data between Linux servers. You can use rename to replace every space with an underscore.

rename 's/ /_/g' *

(Note: The g at the end of the expression stands for “global,” meaning it will replace every space in the file name, not just the first one it finds).

3. Converting Uppercase to Lowercase

Linux is strictly case-sensitive. If you receive a batch of files with chaotic, mixed-case names (e.g., InVoice_JAN.PDF), you can force them all into neat, predictable lowercase letters.

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

(Note: The y operator translates one set of characters into another. In this case, it maps every uppercase letter to its lowercase equivalent).

4. Adding a Prefix to Multiple Files

If you have a folder of generic image names (e.g., 001.jpg, 002.jpg) and want to add a descriptive prefix to all of them, you can search for the start of the file name and append text.

rename 's/^/vacation_/' *.jpg

(Note: The ^ symbol represents the absolute beginning of the file name string).

How to Test Your Command Safely (The Dry Run)

Bulk renaming can be dangerous. If you execute a poorly written regular expression, you could accidentally strip the extensions off critical system files, rendering them unreadable.

Before you run any rename command, you should always perform a “dry run.” By adding the -n flag, the utility will simulate the operation and print the proposed changes to your terminal screen, but it will not actually modify any files on your hard drive.

rename -n 's/ /_/g' *

Review the terminal output carefully. If the proposed file names look correct, simply press the UP arrow on your keyboard to recall the command, delete the -n flag, and press Enter to execute the changes for real.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

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

Receive our best articles and tips delivered straight to your inbox.