rename Command: Bulk Renaming Files
What you'll be able to do
- Preview a rename with `rename -n` before running it for real
- Replace extensions and text in bulk with `s/old/new/`
- Tell the Perl and util-linux versions apart with `rename --version` and switch syntax
Prerequisites (read these first)
What You'll Learn
- How to rename many files at once with a single command
- The safe habit of previewing changes with
-nbefore anything is renamed - How
s/old/new/substitution swaps extensions or text in bulk - How to tell apart the two different rename commands (Perl vs util-linux)
Words used in this article
- Rename: changing the name of a file. Nothing else about the file changes.
- Extension: the
.jpgor.txtpart at the end of a file name. - Pattern: a way of writing "look for text shaped like this". Here it takes the form
s/old/new/. - Dry run: showing the plan without actually doing anything. In this article,
-nis the dry run. - Quoting: wrapping text in
'. Inside quotes, symbols are passed through as plain characters. - Current directoryA container that organizes files. Same idea as a "folder" on Windows or macOS.: the directory you are in right now. Read it as "where you are".
rename rewrites file names. That is exactly why you learn -n first. While -n is on, not a single file changes.
Quick Summary (the patterns to remember first)
- Preview first →
rename -n 's/old/new/' *(shows the plan, changes nothing) - Then run it →
rename 's/old/new/' * - Uppercase to lowercase →
rename 'y/A-Z/a-z/' * - Not sure which rename you have →
rename --version
Assumptions (target environment)
- OS: Ubuntu / Debian. The default
renameis the Perl version - On other distributions (Fedora and so on) you may have the util-linux version. Its syntax differs (covered in Section 6)
Three habits that make practice safe
A wrong rename changes several file names at once. The old names do not come back on their own. These three habits keep practice safe.
- Create a practice directory and work only inside it (for example,
mkdir -p ~/rename-practice && cd ~/rename-practice) - Test on throwaway files, such as empty ones created with
touch - Always add
-nbefore the real run and read the planned changes
Note that rename is not part of the virtual terminalAn interactive program that reads the commands you type and runs them. on this site. Try it on a real Linux machine or WSL. That is exactly why the three habits above matter.
1. What Is the rename Command?
Conclusion: rename changes the names of many files at once using a pattern, removing the need to run mv one file at a time.
photo1.jpeg and photo2.jpeg. I want to change every extension to .jpg.mv?rename is for.mv is a tool for renaming one file. rename is a tool for renaming lots of files at once.mv 50 times?.jpeg part with .jpg". That is its real strength.The basic idea of rename
rename 's/jpeg/jpg/' *.jpeg
↑ ↑
| target files (here, every .jpeg)
rule (replace jpeg with jpg)It does one thing: it finds jpeg in a file name and rewrites it to jpg. It does that across all the target files at once.
2. Why Use rename?
Conclusion: mv is fine for one file, but inefficient for dozens under the same rule. rename lets you write the pattern once.
mv, right? Why bother learning rename?mv old.txt new.txt, mv is plenty.mv you would type the command 100 times.rename you write the rule once and it applies to everything.2026_ to the start of every name" or "turn spaces into underscores" finish in one shot. There are fewer typing mistakes too.Renaming files is hard to undo. Typing mv 50 times invites typos.
Folding the work into one rename rule leaves just one thing to check, which is safer.
There is a flip side. A wrong rule changes all 50 names at once. That is why the -n preview in the next section is essential.
3. Always Preview With -n First (the key habit)
Conclusion: Always run rename with -n first to see exactly what would change, then drop -n to apply it for real.
The most important thing with rename is to never run it blindly. Add -n first. It is the short form of --nono, the no-action mode.
With -n, only the plan of changes is displayed.
$ rename -n 's/jpeg/jpg/' *.jpeg
rename(photo1.jpeg, photo1.jpg) rename(photo2.jpeg, photo2.jpg) rename(photo3.jpeg, photo3.jpg)
rename(photo1.jpeg, photo1.jpg). Have the files actually changed?-n is on, not one file has changed.rename(before, after).-n and run the same command again. That is when the rename actually happens.Once the plan looks correct, drop -n to run it for real.
$ rename 's/jpeg/jpg/' *.jpeg
Now the files are actually renamed. Add -v (--verbose) to print the names it changed.
$ rename -v 's/jpeg/jpg/' *.jpeg
photo1.jpeg renamed as photo1.jpg photo2.jpeg renamed as photo2.jpg
The golden rule
- Run
rename -n 'rule' targetsto check the plan - If the output is correct, drop
-nand run it - If you are unsure, add
-vto see the result as well
Always keep this two-step habit: preview, then run.
4. Replacing Text With s///
Conclusion: 's/before/after/' is the most common form. It replaces text inside file names with other text.
Ubuntu's rename (the Perl version) takes a substitution rule like 's/old/new/'. This comes from the Perl language, where s stands for substitute.
# Change extension .jpeg to .jpg $ rename -n 's/jpeg/jpg/' *.jpeg # Change "draft" to "final" in names $ rename -n 's/draft/final/' * # Add "2026_" to the start (^ means start of the name) $ rename -n 's/^/2026_/' * # Add ".bak" to the end ($ means end of the name) $ rename -n 's/$/.bak/' *
s at the start of s/jpeg/jpg/? It is separated by slashes.s is the signal for "substitute". s/A/B/ means "find A and replace it with B"./ are separators. Just remember s/before/after/.^ and $. What are those?^ is the start of the name and $ is the end of the name.s/^/2026_/ means "add 2026_ at the front". And s/$/.bak/ means "add .bak at the end".Common substitution patterns
| What you want | Rule |
|---|---|
| Change the extension | s/jpeg/jpg/ |
| Swap a string | s/draft/final/ |
| Add text to the start | s/^/prefix_/ |
| Add text to the end | s/$/_suffix/ |
| Delete a string | s/_copy// |
Making the replacement empty (//) means "delete that string."
5. Normalizing Case With y///
Conclusion: 'y/A-Z/a-z/' converts uppercase to lowercase in bulk. It maps characters one-to-one.
To make file names lowercase, use 'y/A-Z/a-z/'. Here y means a character-level translation, called transliterate in English.
# Make all uppercase letters lowercase $ rename -n 'y/A-Z/a-z/' * # Make all lowercase letters uppercase $ rename -n 'y/a-z/A-Z/' *
IMG_001.JPG with mixed-in uppercase. I want them all lowercase.y/A-Z/a-z/ is perfect. It replaces each A-Z with the matching a-z, one character at a time.IMG_001.JPG becomes img_001.jpg.s///?s/// replaces a chunk of text. y/// replaces one character at a time using a mapping table.y/// is the right fit.5-1. Lina Gets Stuck: One File Refused to Change
File.txt and file.txt. I ran rename 'y/A-Z/a-z/' * and got a message I had never seen.$ ls
File.txt file.txt
$ rename 'y/A-Z/a-z/' *
File.txt not renamed: file.txt already exists
$ ls
File.txt file.txt
file.txt. A file with that name already existed.rename (the Perl version) stops instead of overwriting when the target name exists. That is why you got not renamed.-f. That cannot be undone, so please do not use -f in this article.not renamed as "that name is already taken".The same check runs even with -n, so you can spot it at the preview stage.
$ rename -n 'y/A-Z/a-z/' *
File.txt not renamed: file.txt already exists
Watch for two files that differ only in case. Lowercasing File.txt and file.txt produces the same name.
By default the second one is skipped, not overwritten. A not renamed line means that file was left alone.
Only -f (--force) overwrites, and the original cannot be recovered. This article never uses -f.
6. Beware of Two Different rename Commands (Perl vs util-linux)
Conclusion: There are two different commands both named rename, with different syntax. Run rename --version to see which one. Ubuntu's default is the Perl version.
There are actually two commands with the same name but different internals. This is the biggest point of confusion for beginners.
rename .jpeg .jpg *.jpeg. Which one is correct?rename --version and you will know.$ rename --version
For the Perl version (Ubuntu / Debian default), you'll see something like File::Rename.
/usr/bin/rename using File::Rename version 1.xx
For the util-linux version, you'll see the words util-linux.
rename from util-linux 2.xx
The two rename commands
| Item | Perl version (file-rename) | util-linux version |
|---|---|---|
| Common on | Ubuntu / Debian | Fedora / RHEL, etc. |
| Syntax | rename 's/A/B/' * |
rename A B files |
| Patterns | Regular expressions | Simple string replace |
| Dry-run | -n |
-n |
A regular expression is a way of describing a sequence of characters as a pattern. The s/A/B/ form used in this article is one kind of it.
This article assumes the Perl version (Ubuntu default). On the util-linux version, the same .jpeg → .jpg change looks like this:
If you're on the util-linux version, the same operation looks like this.
# util-linux version: rename FROM TO target-files $ rename .jpeg .jpg *.jpeg
If the Perl version is not installed on Ubuntu, add it with sudo apt install rename. Installing requires admin privileges.
Either way, check rename --version first.
7. Common Beginner Pitfalls
Conclusion: Running without -n, targeting too many files, and mixing up the Perl and util-linux versions are the top three mistakes.
7-1. Running without -n
# Bad: run immediately with no check (a wrong rule hits everything) $ rename 's/a/b/' * # Good: preview with -n first $ rename -n 's/a/b/' *
Renaming is hard to undo. Always look at the plan with -n before the real run. This alone prevents most accidents.
7-2. Targeting too many files
# Risky: every file in the current directory is a target $ rename -n 's/o/0/' * # Safe: narrow the target to .txt $ rename -n 's/o/0/' *.txt
* matches every file in the current directory. Unintended files get caught that way. Narrow the range with something like *.txt.
7-3. Using the util-linux version while expecting the Perl version
rename 's/jpeg/jpg/' *.jpeg but nothing changed.s/.../.../ form.rename --version. If it is util-linux, switch to the rename .jpeg .jpg *.jpeg form.8. Mini Exercises: Try It Yourself
Conclusion: Create practice files, then try substitution, prefixing, and lowercasing in three exercises, each with -n.
touch have no contents, so losing one costs nothing.Set up practice files.
$ mkdir -p ~/rename-practice && cd ~/rename-practice $ touch photo1.JPEG photo2.JPEG report_draft.txt
Exercise 1: Show what changing .JPEG to .jpg would do, without running it.
Show Hint 1 (Direction)
You are not running it yet. You only want the plan of what would happen.
That takes one extra option.
Show Hint 2 (Command name)
Use rename. The plan-only option is -n. Write the rule in the form 's/before/after/'.
Target the files with *.JPEG.
Show Answer
$ rename -n 's/JPEG/jpg/' *.JPEG
rename(photo1.JPEG, photo1.jpg) rename(photo2.JPEG, photo2.jpg)
Seeing the plan like this means it worked. Not a single file has changed yet.
Exercise 2: Change draft to final in report_draft.txt. Check the plan, then run it for real.
Show Hint 1 (Direction)
You run the same command twice. The first run checks the plan and the second one applies it.
For the second run, drop the preview option.
Show Hint 2 (Command name)
The rule is 's/draft/final/'. Add -n the first time and drop -n the second time.
You can name the target file directly.
Show Answer
$ rename -n 's/draft/final/' report_draft.txt $ rename 's/draft/final/' report_draft.txt $ ls
photo1.JPEG photo2.JPEG report_final.txt
It worked if the file became report_final.txt.
Exercise 1 stopped at -n, so the .JPEG files are still untouched. That is the "nothing changes while -n is on" rule, visible again.
Exercise 3: Make all remaining file names lowercase.
Show Hint 1 (Direction)
This maps one character at a time instead of replacing a chunk of text. It uses a different letter from s///.
Check the plan with -n first and watch for any not renamed lines.
Show Hint 2 (Command name)
Use y/A-Z/a-z/. Write it after rename, wrapped in quotes. The target is *.
Show Answer
$ rename -n 'y/A-Z/a-z/' * $ rename 'y/A-Z/a-z/' *
A not renamed: ... already exists line means that file was left unchanged. It is the signal that the target name is already taken.
9. Cleaning Up the Practice Directory
Cleanup uses rm, and rm is different from everything else here
A file removed with rm does not go to a trash folder the way it does in a GUI. It is gone immediately and cannot be recovered.
-r: removes a directory together with its contents-i: asks about each item before removing it
With -i, a confirmation appears for every target. Type y and press Enter to remove it, or type n to keep it.
Before deleting, always check the pathA string that describes the location of a file or directory. and contents with ls.
$ cd ~ $ ls ~/rename-practice $ rm -ri ~/rename-practice
10. Copy-Paste Templates
Conclusion: Keep the common forms for previewing, substituting, prefix/suffix, lowercasing, and checking the version handy.
Common forms (Perl version assumed)
# Preview the plan first (most important) rename -n 's/old/new/' * # Run for real after checking rename 's/old/new/' * # Change an extension rename -n 's/jpeg/jpg/' *.jpeg # Add text to the start rename -n 's/^/2026_/' * # Add text to the end rename -n 's/$/.bak/' * # Delete a string rename -n 's/_copy//' * # Normalize uppercase to lowercase rename -n 'y/A-Z/a-z/' * # Check which rename you have rename --version
11. Review
rename I write the rule once and many names change together.-n first and read the plan with my own eyes.File.txt and file.txt accident would have shown up in that plan too.Today's 3-Line Summary
renameapplies one rule to many file names at once- Always add
-nbefore the real run. It prints the plan and changes nothing s/old/new/replaces a chunk of text, andy/A-Z/a-z/maps one character at a time