Batch Rename Files in Linux: 6 Ways
In this article
The simplest ways to batch rename files in Linux are the rename command, a shell for loop, and mmv; each handles bulk renaming differently depending on whether you need Perl expressions, simple pattern swaps, or interactive safety. This article covers those three plus the mv trick, qmv for editing in a text editor, and GUI tools, so you can pick the right one for your task.
Why Batch Renaming Is Tricky in Linux
Linux has no single "rename files" dialog like Windows Explorer. The core mv command moves one file at a time, and while you can script it, the shell's filename expansion (globbing) and quoting rules make bulk operations error-prone. Spaces, special characters, and overlapping names (like renaming a.txt to b.txt when b.txt already exists) cause silent overwrites or failures. Understanding the tools below helps you avoid those pitfalls.
Method 1: The rename Command (Perl Version)
Most Debian/Ubuntu systems ship a Perl-based rename that takes a Perl expression as its first argument. It's the most powerful for complex patterns.
- Syntax:
rename 's/old/new/' files... - Example:
rename 's/\.txt$/\.md/' *.txtchanges all.txtfiles to.md. - Use
-nto dry-run:rename -n 's/foo/bar/' *shows what would change without doing it. - Use
-vto print each rename as it happens.
Note: Red Hat/Fedora systems use a different rename (from util-linux) that only does simple string substitutions, not Perl expressions. Check with rename --version; if it shows "util-linux", use the loop method below instead.
Method 2: Shell for Loop with mv
When rename isn't available or you need custom logic, a for loop in bash works everywhere.
- Basic loop:
for f in *.jpg; do mv "$f" "${f%.jpg}_backup.jpg"; done - This adds
_backupbefore the extension for every.jpgfile. - Use
${f%pattern}to remove a suffix,${f#pattern}to remove a prefix, and${f/old/new}to replace a substring. - Always quote
"$f"to handle spaces in filenames.
Example: rename IMG_001.jpg to photo_001.jpg with for f in IMG_*.jpg; do mv "$f" "${f/IMG_/photo_}"; done.
Method 3: mmv (Move Multiple Files)
mmv is a dedicated tool that uses wildcard patterns to rename many files at once. It's not installed by default; install it with sudo apt install mmv (Debian/Ubuntu) or your package manager.
- Syntax:
mmv '*.txt' '#1.md'renames all.txtfiles to.md. - The
#1refers to the first wildcard*;#2would be the second. - Example:
mmv 'IMG_*.jpg' 'photo_#1.jpg'turnsIMG_001.jpgintophoto_001.jpg. - Use
-nfor a dry run,-mto move, and-oto overwrite existing files (default is to skip).
mmv is safer than a loop because it checks for collisions before renaming anything.
Method 4: qmv (Edit Names in a Text Editor)
qmv (from the renameutils package) opens a text editor with a list of filenames. You edit the names, save, and it renames them accordingly.
- Install:
sudo apt install renameutils. - Run
qmv *.jpg– your editor opens with two columns: original names and new names (initially identical). - Edit the second column, save, and exit.
qmvperforms the renames.
This is ideal for complex, manual changes that are hard to express with a single pattern.
Method 5: GUI Tools (Thunar, GPRename, KRename)
If you prefer a graphical interface, file managers and dedicated tools offer point-and-click batch renaming.
- Thunar (Xfce's file manager) has a built-in "Rename Multiple Files" option: select files, press F2, and add prefixes, suffixes, search/replace, or renumber.
- GPRename is a standalone GTK tool with similar features.
- KRename is the KDE equivalent, very feature-rich.
These are great for occasional use and for seeing a preview before applying changes.
Common Pitfalls and Safety Tips
- Overwrites:
mvoverwrites existing files silently. Use-i(interactive) or-n(no-clobber) to avoid data loss. - Dry-run first: Always test with
-n(forrename,mmv) or echo themvcommand in a loop before executing. - Spaces and special chars: Always quote variables in loops. Use
find -print0withxargs -0for filenames with newlines (rare but possible). - Case sensitivity: Linux filenames are case-sensitive, so
File.txtandfile.txtare different. Renaming only the case can fail if the target already exists. - Extension changes: Renaming
.txtto.mddoesn't convert content; it only changes the name.
Frequently Asked Questions
How do I rename files with a sequential number?
Use a loop with a counter: i=1; for f in *.jpg; do mv "$f" "photo_$(printf '%03d' $i).jpg"; i=$((i+1)); done. This creates photo_001.jpg, photo_002.jpg, etc. The printf '%03d' pads to three digits.
What's the difference between rename on Ubuntu and Fedora?
Ubuntu's rename is a Perl script that accepts regular expressions. Fedora's rename (from util-linux) only does simple string substitution: rename old new files. Check with rename --version to see which you have.
How do I rename files in subdirectories recursively?
Use find with -exec or a loop: find . -type f -name '*.txt' -exec rename 's/\.txt$/\.md/' {} \;. For a loop, use find . -type f -name '*.txt' -print0 | while IFS= read -r -d '' f; do mv "$f" "${f%.txt}.md"; done.
Can I undo a batch rename?
No, there's no built-in undo. Always dry-run first and consider making a backup of filenames: ls > filelist.txt before renaming, so you can manually revert if needed.
Why does rename say "rename: not enough arguments"?
You likely forgot the expression or the file list. The Perl rename requires at least two arguments: the expression and at least one filename. For example, rename 's/foo/bar/' *.txt works, but rename 's/foo/bar/' alone fails.