rename Command: Bulk Renaming Files

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 -n before 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 .jpg or .txt part 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, -n is 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 rename is 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.

  1. Create a practice directory and work only inside it (for example, mkdir -p ~/rename-practice && cd ~/rename-practice)
  2. Test on throwaway files, such as empty ones created with touch
  3. Always add -n before 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.

Lina: Senpai, I have 50 files like photo1.jpeg and photo2.jpeg. I want to change every extension to .jpg.
Lina: Do I have to fix them one by one with mv?
Linny-senpai: Doing 50 by hand would be painful. That is exactly what rename is for.
Linny-senpai: mv is a tool for renaming one file. rename is a tool for renaming lots of files at once.
Lina: So I do not have to type mv 50 times?
Linny-senpai: Right. One command renames all 50.
Linny-senpai: And you can specify a pattern, like "replace just the .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.

Lina: I can already rename with mv, right? Why bother learning rename?
Linny-senpai: Good question. For renaming a single file, like mv old.txt new.txt, mv is plenty.
Linny-senpai: The problem is changing 100 files by the same rule. With mv you would type the command 100 times.
Lina: That really is not practical.
Linny-senpai: With rename you write the rule once and it applies to everything.
Linny-senpai: Tasks like "add 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)
Lina: It printed rename(photo1.jpeg, photo1.jpg). Have the files actually changed?
Linny-senpai: No. While -n is on, not one file has changed.
Linny-senpai: It is a preview that says "if you ran this, here is what would change". It prints as rename(before, after).
Lina: So it shows me the plan first. That is reassuring.
Linny-senpai: When the output looks right, drop -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

  1. Run rename -n 'rule' targets to check the plan
  2. If the output is correct, drop -n and run it
  3. If you are unsure, add -v to 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/' *
Lina: What is the s at the start of s/jpeg/jpg/? It is separated by slashes.
Linny-senpai: The s is the signal for "substitute". s/A/B/ means "find A and replace it with B".
Linny-senpai: The slashes / are separators. Just remember s/before/after/.
Lina: I also see ^ and $. What are those?
Linny-senpai: Those are position markers. ^ is the start of the name and $ is the end of the name.
Linny-senpai: So s/^/2026_/ means "add 2026_ at the front". And s/$/.bak/ means "add .bak at the end".
Linny-senpai: 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 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/' *
Lina: I have files like IMG_001.JPG with mixed-in uppercase. I want them all lowercase.
Linny-senpai: Then y/A-Z/a-z/ is perfect. It replaces each A-Z with the matching a-z, one character at a time.
Linny-senpai: So IMG_001.JPG becomes img_001.jpg.
Lina: Is that different from s///?
Linny-senpai: s/// replaces a chunk of text. y/// replaces one character at a time using a mapping table.
Linny-senpai: When you want to change a whole class of characters, like upper to lower case, y/// is the right fit.

5-1. Lina Gets Stuck: One File Refused to Change

Lina: In my practice folder I created 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
Lina: I expected both to end up lowercase. One of them did not change.
Linny-senpai: Lowercasing gives the name file.txt. A file with that name already existed.
Linny-senpai: Ubuntu's rename (the Perl version) stops instead of overwriting when the target name exists. That is why you got not renamed.
Lina: So it does not silently overwrite. I did not expect that.
Linny-senpai: Right. It only overwrites when you add -f. That cannot be undone, so please do not use -f in this article.
Lina: Understood. I will read 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.

Lina: I searched online and found examples written without slashes, like rename .jpeg .jpg *.jpeg. Which one is correct?
Linny-senpai: Both are correct. But they are different rename commands. There are mainly two kinds.
Lina: The same name but different internals?
Linny-senpai: Right. So the first thing to do is confirm which one you have. Run 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

Lina: I ran rename 's/jpeg/jpg/' *.jpeg but nothing changed.
Linny-senpai: You are probably on the util-linux version. It does not understand the s/.../.../ form.
Linny-senpai: Check with 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.

Lina: I want to try it hands-on. But I am scared of breaking real files.
Linny-senpai: That is why we make empty practice files. Files made with touch have no contents, so losing one costs nothing.
Linny-senpai: Create a practice directory first and work only inside it.

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

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

Lina: Let me sum up. With rename I write the rule once and many names change together.
Linny-senpai: Right. And that is exactly why a wrong rule changes everything at once.
Lina: So I add -n first and read the plan with my own eyes.
Linny-senpai: Well remembered. The File.txt and file.txt accident would have shown up in that plan too.

Today's 3-Line Summary

  1. rename applies one rule to many file names at once
  2. Always add -n before the real run. It prints the plan and changes nothing
  3. s/old/new/ replaces a chunk of text, and y/A-Z/a-z/ maps one character at a time

Next Reading

Share this article

Next steps