rename Command in Linux: Rename Multiple Files

By 

•

Updated on

•

10 min read

Terminal showing rename command renaming multiple files

The rename command is a tool for batch renaming files using pattern matching. Unlike the mv command, which renames one file at a time, rename can change the names of multiple files in a single operation using Perl regular expressions or simple string substitution.

This guide explains how to install and use the rename command to rename files and directories in Linux.

rename Implementations

Linux distributions package several commands named rename. They serve the same general purpose, but their options are not identical:

  • File::Rename (rename or file-rename) - Uses Perl expressions and provides options such as -E and -d. Ubuntu and Debian package this implementation, which is the primary version covered in this guide.
  • Peder Stray rename (prename on Fedora or perl-rename on Arch Linux) - Also uses Perl expressions, but its command-line options differ from File::Rename.
  • util-linux rename (rename or rename.ul) - Uses simple string substitution (rename oldstr newstr files) instead of Perl expressions.

To check the version of the command installed on your system, run:

Terminal
rename --version

Output that mentions “File::Rename” identifies the first Perl implementation. If it shows “util-linux”, you have the C version. The prename and perl-rename command names identify the Peder Stray implementation.

Installing the Perl rename

If the Perl version is not installed on your system, use the package manager of your Linux distribution to install it:

  • Ubuntu, Debian, and Derivatives

    Terminal
    sudo apt update
    sudo apt install rename
  • Fedora, RHEL, and Derivatives

    Terminal
    sudo dnf install prename

    The package is available directly on Fedora. On RHEL 8 and 9, you must enable EPEL before installing it. The package is not currently available through EPEL 10.

  • Arch Linux

    Terminal
    sudo pacman -S perl-rename

The name of the installed command depends on your distribution. On Ubuntu and Debian, the package provides rename and file-rename. On Fedora, rename is the util-linux version, and the Perl version is installed as prename. On Arch Linux, it is installed as perl-rename.

The basic Perl-expression examples in this guide work with all of these Perl command names. Examples that require File::Rename-specific options are labeled and should not be copied directly to prename or perl-rename.

Syntax

The general syntax for the Perl rename command is:

txt
rename [OPTIONS] perlexpr files
  • perlexpr - A Perl regular expression that defines how to transform the file names.
  • files - One or more files or a glob pattern to match.

The most common form uses the substitution operator s/pattern/replacement/:

txt
rename 's/search/replace/' files

Renaming Files

The following command changes the extension of all .css files to .scss:

Terminal
rename 's/\.css/.scss/' *.css

Here is what each part does:

  • s/search/replace/ - The Perl substitution operator.
  • \.css - The search pattern. The backslash escapes the dot so it matches a literal . character.
  • .scss - The replacement string.
  • *.css - A shell glob that expands to all files with the .css extension.

Dry Run with -n

Before running the actual command and renaming the files, it is a good practice to use the -n option to perform a dry run and see what files will be renamed:

Terminal
rename -n 's/\.css/.scss/' *.css

With File::Rename, the output looks like this:

output
rename(file-0.css, file-0.scss)
rename(file-1.css, file-1.scss)
rename(file-2.css, file-2.scss)
rename(file-3.css, file-3.scss)
rename(file-4.css, file-4.scss)

Each line shows the current name followed by the new name. Nothing is renamed until you run the command again without -n.

File::Rename also reports conflicts during a dry run. If file-0.scss already exists, you will see file-0.css not renamed: file-0.scss already exists in place of the first line.

Verbose Output with -v

To print the names of files that are successfully renamed, use the -v (verbose) option:

Terminal
rename -v 's/\.css/.scss/' *.css
output
file-0.css renamed as file-0.scss
file-1.css renamed as file-1.scss
file-2.css renamed as file-2.scss
file-3.css renamed as file-3.scss
file-4.css renamed as file-4.scss

Overwrite Existing Files with -f

The -f option permits overwriting an existing target without prompting. Because the default overwrite behavior differs between implementations, use a dry run first and add -f only when replacing the destination is intentional:

Terminal
rename -f 's/\.css/.scss/' *.css

Multiple Expressions in File::Rename

File::Rename provides the -E option for chaining multiple rename operations in one command. Each expression is applied in order:

Terminal
rename -E 'y/A-Z/a-z/' -E 's/\.jpeg$/.jpg/' *.jpeg *.JPEG

This first converts the entire filename to lowercase, then changes the extension from .jpeg to .jpg. A file named PHOTO.JPEG becomes photo.jpg. The order matters here. If the extension is changed first, the .JPEG suffix does not match \.jpeg$, and the file ends up as photo.jpeg.

There is also a lowercase -e option. It joins the expressions with a newline, the same way perl -e does, so each expression except the last one must end with a semicolon (-e 's/a/b/;'). The -E option adds the semicolon for you.

The Fedora and Arch Linux implementations do not support -E. Combine the statements inside one Perl expression instead. On Fedora, run:

Terminal
prename 'y/A-Z/a-z/; s/\.jpeg$/.jpg/' *.jpeg *.JPEG

On Arch Linux, use perl-rename in place of prename.

Renaming Only the Filename with File::Rename

By default, File::Rename applies the expression to the whole path it receives, including the directory names. Use its -d option to change only the filename and leave the directories alone:

Terminal
rename -n -d 's/ /_/g' "old dir/old file.txt"
output
rename(old dir/old file.txt, old dir/old_file.txt)

Without -d, the space in the directory name would be replaced too, and rename would try to move the file to old_dir/old_file.txt. That directory does not exist, so the rename fails.

The prename and perl-rename commands do not support -d. For recursive operations, use the find -execdir method shown below so the command runs inside each file’s directory.

Examples

Below are common examples of how to use the rename command.

Replace Spaces with Underscores

The y operator translates characters one by one. The following command replaces every space in the filenames with an underscore:

Terminal
rename 'y/ /_/' *

Convert Filenames to Lowercase

To convert all uppercase letters in the filenames to lowercase, run:

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

Convert Filenames to Uppercase

The same operator with the ranges swapped converts the filenames to uppercase:

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

Remove .bak Extension

To strip the .bak extension from backup files, replace it with an empty string:

Terminal
rename 's/\.bak$//' *.bak

The $ anchors the match to the end of the name, so a file like app.bak.conf is not touched.

Rename .jpeg and .JPG to .jpg

The i flag at the end makes the match case-insensitive:

Terminal
rename 's/\.jpe?g$/.jpg/i' *

The e? makes the e optional, so the pattern matches .jpg, .jpeg, .JPG, and .JPEG.

Add a Prefix to All Files

The ^ anchor matches the start of the name. Use it to add a prefix to all .txt files:

Terminal
rename 's/^/project_/' *.txt

Remove a Prefix from Filenames

To remove the prefix, match it at the start of the name and replace it with nothing:

Terminal
rename 's/^old_//' old_*.txt

Insert a Number Sequence

Use a counter variable to number files sequentially:

Terminal
rename -v 'our $i; s/^/sprintf("%03d_", ++$i)/e' *.jpg

The e flag tells Perl to evaluate the replacement as code. The first file gets the 001_ prefix, the second 002_, and so on.

Safe Recursive Rename with find

To rename matching files in subdirectories without changing their parent directory names, use find with -execdir. This command performs a dry run inside each file’s directory:

Terminal
find . -type f -name '*.txt' -execdir rename -n 's/ /_/g' {} +

On Fedora, replace rename with prename. On Arch Linux, use perl-rename. Because -execdir runs the command from the directory containing each match, the expression changes only the filename. Remove -n after you verify the dry-run output.

Quick Reference

CommandDescription
rename 's/old/new/' filesReplace first occurrence of old with new
rename 's/old/new/g' filesReplace all occurrences of old with new
rename -n 's/old/new/' filesDry run - show what would be renamed
rename -v 's/old/new/' filesVerbose - print renamed files
rename -f 's/old/new/' filesForce overwrite existing files
rename 'expr1; expr2' filesApply multiple Perl expressions in one argument
rename -E 'expr1' -E 'expr2' filesApply multiple expressions with File::Rename
rename -d 's/old/new/' pathsRename only the filename with File::Rename
rename 'y/A-Z/a-z/' *Convert filenames to lowercase
rename 'y/ /_/' *Replace spaces with underscores
rename 's/\.bak$//' *.bakRemove .bak extension
rename 's/^/prefix_/' *Add prefix to all filenames

Troubleshooting

rename: command not found
The Perl rename is not installed by default on all distributions. Install it using the package manager commands in the Installing the Perl rename section above.

Bareword "foo" not allowed while "strict subs" in use
The Perl rename treats its first argument as Perl code. This error appears when you pass it the util-linux syntax, for example rename foo bar *. Write the change as a substitution instead: rename 's/foo/bar/' *.

syntax error at line 2 when using File::Rename -e
Multiple -e expressions are joined with a newline, and Perl expects a semicolon between them. Use -E for each expression, or end each -e expression except the last one with ;.

unrecognized option for -e, -E, or -d
You are using prename or perl-rename, which does not provide these File::Rename options. Combine multiple statements inside one Perl expression, and use find -execdir when you need to rename files recursively without changing directory names.

Files are not renamed but no error is shown
The regular expression did not match any part of the filenames. Use the -n flag to test the expression and verify it matches the intended files.

If rename -n prints nothing for an expression that should match, you are likely running the C version from util-linux. It uses a different syntax (rename oldstr newstr files) and looks for the literal string s/old/new/ in the filenames. Check your version with rename --version and install the Perl version if needed.

not renamed: ... already exists warning
The target filename already exists, and rename skips the file instead of overwriting it. Use -f to force overwriting, or adjust the pattern to produce unique filenames.

FAQ

What is the difference between mv and rename?
The mv command renames or moves a single file at a time. The rename command uses Perl regular expressions to batch rename multiple files in one operation, making it far more efficient for bulk operations.

How do I rename files recursively in subdirectories?
The rename command does not traverse directories on its own. Combine it with find to rename files recursively: find . -type f -name '*.txt' -execdir rename -n 's/old/new/' {} +. The -type f test excludes directories, while -execdir runs the command inside each file’s directory. Replace the command name on Fedora or Arch Linux, and remove -n after checking the preview.

Can I undo a rename operation?
There is no built-in undo. Always use rename -n for a dry run first. For important bulk changes, keep a backup or save the original-to-new name mapping so you can restore the exact names. Swapping the search and replacement strings is not a reliable undo method because it can affect unrelated files or create name conflicts.

How do I rename files with special characters in their names?
Use single quotes around the Perl expression to prevent shell interpretation. For filenames with spaces or special characters, the shell glob or find command handles the file matching.

Conclusion

The rename command is a powerful tool for batch renaming files using Perl regular expressions. Always test with -n before running the actual rename to avoid unintended changes.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page