rename Command in Linux: Rename 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 (
prenameorrename) — Uses Perl regular expressions for powerful pattern matching. This is the version covered in this guide. - C version (
rename.ulfrom 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:
rename --versionIf 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
Terminalsudo apt update sudo apt install renameFedora, RHEL, and Derivatives
Terminalsudo dnf install prenameArch Linux
Terminalsudo pacman -S perl-rename
Syntax
The general syntax for the Perl rename command is:
rename [OPTIONS] perlexpr filesperlexpr— 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/:
rename 's/search/replace/' filesRenaming Files
The following command changes the extension of all .css files to .scss:
rename 's/\.css/.scss/' *.cssHere 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.cssextension.
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:
rename -n 's/\.css/.scss/' *.cssrename(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:
rename -v 's/\.css/.scss/' *.cssfile-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.scssOverwrite Existing Files with -f
By default, the rename command does not overwrite existing files. Use the -f option to force overwriting:
rename -f 's/\.css/.scss/' *.cssMultiple Expressions with -e
Use the -e option to chain multiple rename operations in a single command. Each expression is applied in order:
rename -e 's/\.jpeg$/.jpg/' -e 'y/A-Z/a-z/' *.jpeg *.JPEGThis 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
rename 'y/ /_/' *Convert Filenames to Lowercase
rename 'y/A-Z/a-z/' *Convert Filenames to Uppercase
rename 'y/a-z/A-Z/' *Remove .bak Extension
rename 's/\.bak$//' *.bakRename .jpeg and .JPG to .jpg
The i flag at the end makes the match case-insensitive:
rename 's/\.jpe?g$/.jpg/i' *Add a Prefix to All Files
rename 's/^/project_/' *.txtRemove a Prefix from Filenames
rename 's/^old_//' old_*.txtInsert a Number Sequence
Use a counter variable to number files sequentially:
rename -v 'our $i; s/^/sprintf("%03d_", ++$i)/e' *.jpgSafe Bulk Rename with find and xargs
When file names may contain spaces or special characters, use null-delimited input to avoid splitting errors:
find . -type f -name '*.txt' -print0 | xargs -0 rename -n 's/ /_/g'Remove -n after you verify the dry-run output.
Quick Reference
| Command | Description |
|---|---|
rename 's/old/new/' files | Replace first occurrence of old with new |
rename 's/old/new/g' files | Replace all occurrences of old with new |
rename -n 's/old/new/' files | Dry run — show what would be renamed |
rename -v 's/old/new/' files | Verbose — print renamed files |
rename -f 's/old/new/' files | Force overwrite existing files |
rename -e 'expr1' -e 'expr2' files | Apply multiple expressions |
rename 'y/A-Z/a-z/' * | Convert filenames to lowercase |
rename 'y/ /_/' * | Replace spaces with underscores |
rename 's/\.bak$//' *.bak | Remove .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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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