Introduction
The rename utility is a powerful command-line tool available on many Linux distributions for performing batch file renaming using Perl regular expressions or simple string substitutions. Whether you need to standardize filenames, apply sequential numbering, change extensions en masse, or normalize case, rename offers a concise, scriptable solution far more flexible than invoking mv repeatedly.
Versions and Compatibility
There are two primary implementations of rename in widespread use:
- Perl-based rename (sometimes called
prename): uses full Perl regular expressions. Common on Debian, Ubuntu and related systems. - util-linux rename: offers simpler syntax (
rename from to files…) and is default on distributions such as Fedora, CentOS and Arch.
Check your version with:
rename --version
For detailed options, consult the rename man page.
Basic Syntax
| Version | Syntax | Example |
|---|---|---|
| Perl-based | rename s/old/new/ files... |
rename s/.txt/.bak/ .txt |
| util-linux | rename from to files... |
rename .txt .bak .txt |
Using Regular Expressions
The Perl-based variant supports the full spectrum of Perl regex:
- Character classes:
[0-9],[A-Za-z] - Grouping amp alternation:
(foobar) - Quantifiers:
, , , {n,m} - Lookarounds:
(=pattern),(!pattern)
Example: prepend IMG_ only to JPEG files:
rename s/^(.).jpeg/IMG_1.jpg/i .JPG
Common Use Cases
1. Changing File Extensions
rename s/.html/.php/ .html
2. Sequential Numbering
Use shell expansion or combine with ls:
a=1 for f in .txt do mv f file_((a )).txt done
Or with renameutils batch editor: renameutils homepage.
3. Case Conversion
- Lowercase all filenames:
rename y/A-Z/a-z/ - Uppercase all filenames:
rename y/a-z/A-Z/
4. Prefix/Suffix Addition
rename s/^/backup_/ .conf
Advanced Techniques
Dry-Run / Preview
Preview changes before applying:
rename -n s/old/new/
The -n flag shows proposed renames without executing them.
Combining with find
Recursively rename across directories:
find . -type f -name .log -exec rename s/.log/.txt/ {}
Undoing with Version Control
If files are tracked in git, you can:
git mv oldname newname
Then commit to maintain history. For bulk renames, use a for loop with git mv.
Troubleshooting and Tips
- Back up critical files before mass renaming.
- Use
-n(dry-run) to prevent mistakes. - Beware of filename collisions: two originals mapping to same target.
- Escape special characters: space, brackets, ampersand.
- When in doubt, wrap filenames in quotes:
file.
Alternative Tools
- mmv: moves/copies/links multiple files by wildcard patterns.
- renameutils: interactive editing of rename commands. See renameutils homepage.
- Thunar Bulk Rename: graphical interface for GTK desktops.
Real-World Examples
Renaming Photo Archives
rename s/^DSC0([0-9] )/photo_1/ DSC.JPG
Standardizing Log Files
rename s/^server_log_([0-9]{4})([0-9]{2})([0-9]{2})/1-2-3.log/ server_log_.log
Conclusion
The rename command is an indispensable tool for system administrators, developers, and power users. Its ability to harness regular expressions for batch processing can save hours of manual work. By mastering syntax, leveraging dry-runs, and combining with other utilities like find, you’ll streamline file management workflows with confidence and precision.
Further reading: consult the Perl Regular Expressions tutorial and your distribution’s package documentation for advanced flags.
Leave a Reply