rename Command in Linux: Rename Multiple Files

By 

Updated on

6 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.

Depending on your distribution, the Perl implementation may be exposed as rename, file-rename, or prename.

Perl vs C Version

There are two versions of the rename command that ship with Linux distributions:

  • Perl version (prename or rename) — Uses Perl regular expressions for powerful pattern matching. This is the version covered in this guide.
  • C version (rename.ul from util-linux) — Uses simple string substitution (rename oldstr newstr files). This version is more limited but comes preinstalled on many systems.

To check which version is installed on your system, run:

Terminal
rename --version

If the output mentions “File::Rename” or “perl”, you have the Perl version. If it shows “util-linux”, you have the C version.

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
  • Arch Linux

    Terminal
    sudo pacman -S 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
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)

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

By default, the rename command does not overwrite existing files. Use the -f option to force overwriting:

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

Multiple Expressions with -e

Use the -e option to chain multiple rename operations in a single command. Each expression is applied in order:

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

This first changes the extension from .jpeg to .jpg, then converts the entire filename to lowercase.

Examples

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

Replace Spaces with Underscores

Terminal
rename 'y/ /_/' *

Convert Filenames to Lowercase

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

Convert Filenames to Uppercase

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

Remove .bak Extension

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

Rename .jpeg and .JPG to .jpg

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

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

Add a Prefix to All Files

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

Remove a Prefix from Filenames

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

Safe Bulk Rename with find and xargs

When file names may contain spaces or special characters, use null-delimited input to avoid splitting errors:

Terminal
find . -type f -name '*.txt' -print0 | xargs -0 rename -n 's/ /_/g'

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 -e 'expr1' -e 'expr2' filesApply multiple expressions
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 found where operator expected or syntax errors You are likely using the C version of rename (from util-linux), which has a different syntax: rename oldstr newstr files. Check your version with rename --version and install the Perl version if needed.

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.

rename: not overwritten warning The target filename already exists. 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 . -name '*.txt' -exec rename 's/old/new/' {} +.

Can I undo a rename operation? There is no built-in undo. Always use rename -n for a dry run first. If you need to reverse a rename, swap the search and replacement strings: if you ran 's/old/new/', reverse it with 's/new/old/'.

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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page