rename Command
Intermediate File Management man(1)Bulk rename files using patterns
๐ 121 views
๐
Updated: May 1, 2026
SYNTAX
rename [OPTIONS] 's/PATTERN/REPLACEMENT/' FILES
rename [OPTIONS] PATTERN REPLACEMENT FILES
What Does rename Do?
The rename command renames multiple files at once using pattern matching โ either Perl regular expressions (Perl rename) or simple string substitution (util-linux rename). It is far more powerful than manual mv commands when you need to rename dozens or hundreds of files following a pattern.
There are two common versions of rename on Linux: the Perl-based rename (also called prename or perl-rename, default on Debian/Ubuntu) which supports full Perl regular expressions, and the util-linux rename (default on RHEL/Fedora) which supports simple string substitution. This guide covers both.
Common use cases include changing file extensions, adding prefixes or suffixes, converting case, removing spaces, numbering files sequentially, and batch-renaming photos, logs, or data files. Combined with find, rename can process files recursively across directory trees.
There are two common versions of rename on Linux: the Perl-based rename (also called prename or perl-rename, default on Debian/Ubuntu) which supports full Perl regular expressions, and the util-linux rename (default on RHEL/Fedora) which supports simple string substitution. This guide covers both.
Common use cases include changing file extensions, adding prefixes or suffixes, converting case, removing spaces, numbering files sequentially, and batch-renaming photos, logs, or data files. Combined with find, rename can process files recursively across directory trees.
Options & Flags
| Option | Description | Example |
|---|---|---|
| 's/old/new/' | Perl rename: substitute old with new using regex | rename 's/\.txt$/.md/' *.txt |
| -n | Dry run - show what would be renamed without doing it | rename -n 's/\.txt$/.md/' *.txt |
| -v | Verbose - show each rename operation | rename -v 's/ /_/g' *.txt |
| -f | Force - overwrite existing files | rename -f 's/old/new/' *.txt |
| 's/old/new/g' | Global - replace all occurrences (not just first) | rename 's/-/_/g' *.log |
| 's/PATTERN/\L$&/' | Convert matching text to lowercase | rename 's/.*/\L$&/' *.TXT |
| 's/PATTERN/\U$&/' | Convert matching text to uppercase | rename 's/.*/\U$&/' *.txt |
| OLD NEW FILES | Util-linux rename: simple string substitution | rename .txt .md *.txt |
Practical Examples
#1 Change file extension
Rename all .txt files to .md. The $ anchor ensures only the extension is matched.
$ rename 's/\.txt$/.md/' *.txt#2 Replace spaces with underscores
Replace all spaces in filenames with underscores. The g flag replaces all occurrences.
$ rename 's/ /_/g' *.pdf#3 Add prefix to all files
Add "2026-" prefix to all .log files. ^ matches the beginning of the filename.
$ rename 's/^/2026-/' *.log#4 Convert filenames to lowercase
Convert all filenames to lowercase using the transliteration operator.
$ rename 'y/A-Z/a-z/' *#5 Remove part of filename
Remove "_backup" from all .sql filenames.
$ rename 's/_backup//' *.sql#6 Dry run first (always recommended)
Preview renames without executing. Shows what would change. Always use -n first for complex patterns.
$ rename -n 's/IMG_(\d+)/photo_$1/' *.jpg
Output:
rename(IMG_001.jpg, photo_001.jpg)
rename(IMG_002.jpg, photo_002.jpg)
#7 Number files sequentially
Rename files to sequential numbers (001.jpg, 002.jpg, ...). Uses printf for zero-padded numbering.
$ ls *.jpg | cat -n | while read n f; do mv "" "$(printf '%03d.jpg' )"; doneTips & Best Practices
Always use -n first: Test with rename -n (dry run) before executing. Complex regex patterns can produce unexpected results. Verify the preview before running without -n.
Two different rename commands: Debian/Ubuntu has Perl rename (regex). RHEL/Fedora has util-linux rename (string substitution). Install Perl version with: apt install rename or dnf install prename.
Use with find for recursive rename: Rename files in subdirectories: find . -name '*.txt' -exec rename 's/\.txt$/.md/' {} +
Backup before bulk rename: For irreversible renames, create a backup first: cp -r dir/ dir.bak/ โ or save the rename mapping: rename -v ... > rename.log
Frequently Asked Questions
How do I rename multiple files at once in Linux?
Use rename: rename 's/old/new/' *.ext โ this applies the substitution to all matching files. Use -n flag to preview first.
How do I replace spaces in filenames?
Use: rename 's/ /_/g' * โ the g flag replaces ALL spaces, not just the first one. Use -n to preview.
Which rename command do I have?
Run: rename --version. Perl rename shows perl version info. util-linux rename shows util-linux version. Or check: file $(which rename).
How do I rename files recursively?
Use find with rename: find . -type f -name '*.txt' -exec rename 's/\.txt$/.md/' {} + โ this processes all .txt files in subdirectories.
Related Commands
More File Management Commands
Download File Management Cheat Sheet
View all 31 Linux command cheat sheets โMaster Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books โ