rename Command: Bulk Renaming Files
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)
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 distros (Fedora, etc.) you may have the util-linux version with different syntax (covered in Section 6)
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, photo2.jpeg... and I want to change every extension to .jpg. Do I have to fix them one by one with mv?rename is for. If mv is "a tool for renaming one file," then rename is "a tool for renaming lots of files at once."mv 50 times?.jpeg part with .jpg," which 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 finds jpeg in each file name and rewrites it to jpg, 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. The problem is when you want to change 100 files by the same rule. With mv you'd type the command 100 times.rename you write the rule once and it applies to everything. Tasks like "add 2026_ to the start of every name" or "turn spaces into underscores" finish in one shot, with fewer hand-typing mistakes.Renaming files is hard to undo. Typing mv 50 times invites typos. Folding the work into one rename rule means there's just one thing to check, which is actually safer. But a wrong rule can break all 50 files at once, so 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 (--nono, the "no action" mode) first to display only the plan of changes.
$ 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), but the files haven't actually changed?-n is on, nothing has changed. It's a preview that says "if you ran this, here's what would change." It shows as rename(before, after).-n and run the same command again. That's 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
rename -n 'rule' targetsto check the plan- If the output is correct, drop
-nand run it - If unsure, add
-vto see the result too
Always keep this two-step "preview then run" habit.
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 is Perl's substitution syntax, where s means 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's separated by slashes.s is the signal for "substitute." s/A/B/ means "find A and replace it with B." The slashes / are separators. Just remember s/before/after/.^ and $. What are those?^ is "the start of the name" and $ is "the end of the name." So s/^/2026_/ means "add 2026_ at the front," and s/$/.bak/ means "add .bak at the end." Don't overthink it at first; these two patterns are enough.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 is a character-level translation (transliterate).
# 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, and I want them all lowercase.y/A-Z/a-z/ is perfect. It replaces each A-Z (uppercase A through Z) with the matching a-z (lowercase a through 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. When you want to change a whole class of characters, like upper to lower case, y/// is the right fit.If two files differ only in case (File.txt and file.txt) and you convert them to the same name, one may be overwritten and lost. Always check the plan with -n before using y/// too.
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'll 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 | Full Perl regex | Simple string replace |
| Dry-run | -n |
-n |
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 isn't installed on Ubuntu, you can 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 running for real. 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. Narrow the range with something like *.txt so unintended files aren't caught.
7-3. Using the util-linux version while expecting the Perl version
rename 's/jpeg/jpg/' *.jpeg but nothing changed.s/.../.../ regex. Check with rename --version, and if it's 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 they have no contents, so it doesn't hurt if they break. Let's make a working folder and experiment.Set up practice files.
$ mkdir rename-practice && cd rename-practice $ touch photo1.JPEG photo2.JPEG report_draft.txt
Exercise 1: Preview (with -n) changing .JPEG to .jpg.
Show hint
Use the rename -n 's/before/after/' targets form. Replace the extension .JPEG with .jpg.
Sample answer
$ rename -n 's/JPEG/jpg/' *.JPEG
It works if the plan shows lines like rename(photo1.JPEG, photo1.jpg).
Exercise 2: Change draft to final in report_draft.txt and actually rename it (preview with -n first).
Show hint
Check with -n first, then drop -n if it looks right.
Sample answer
$ rename -n 's/draft/final/' report_draft.txt $ rename 's/draft/final/' report_draft.txt
It works if it becomes report_final.txt.
Exercise 3: Make all remaining file names lowercase.
Show hint
Character-level translation is y/A-Z/a-z/.
Sample answer
$ rename -n 'y/A-Z/a-z/' * $ rename 'y/A-Z/a-z/' *
9. 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