rename — Batch-Rename Files with Patterns
Rename multiple files using Perl regular expressions (perl-rename). Powerful batch renaming with regex support for case changes, numbering, and pattern substitution.
rename renames many files at once, making it the big sibling of mv as soon as regular expressions come into play. Be warned – and this is the single most important fact on this page: there are two completely different programs called rename. This page covers the Perl rename (also file-rename or prename, the default on Debian/Ubuntu), which works with Perl expressions such as rename 's/old/new/' *.txt. RHEL, Fedora, and SUSE, by contrast, ship the util-linux rename, which has an incompatible syntax (rename old new *.txt, a plain string replacement with no regex). Check which one you have with rename --version, and always try commands with -n (dry run) first before letting them loose.
Basic Usage
rename 's/<old>/<new>/' <files> — Replace first occurrence of a pattern in filenames.
rename 's/foo/bar/' *.txtrename 's/<old>/<new>/g' <files> — Replace all occurrences in filenames.
rename 's/-/_/g' *.htmlrename -n 's/<old>/<new>/' <files> — Dry run: show what would be renamed without doing it.
rename -n 's/.jpeg/.jpg/' *.jpegrename -v 's/<old>/<new>/' <files> — Verbose: show each rename operation.
rename -v 's/IMG_/photo_/' *.jpgrename -f 's/<old>/<new>/' <files> — Force: overwrite existing files.
rename -f 's/backup_//' *.sqlCase Changes
rename 'y/A-Z/a-z/' <files> — Convert filenames to lowercase.
rename 'y/A-Z/a-z/' *.JPGrename 'y/a-z/A-Z/' <files> — Convert filenames to uppercase.
rename 'y/a-z/A-Z/' *.txtrename 's/(\w+)/\L$1/' <files> — Lowercase entire filename using Perl modifiers.
rename 's/(\w+)/\L$1/' *rename 's/(\w)/\u$1/' <files> — Capitalize first letter of filename.
rename 's/(\w)/\u$1/' *.txtExtension Changes
rename 's/\.txt$/.md/' <files> — Change file extension from .txt to .md.
rename 's/\.txt$/.md/' *.txtrename 's/\.jpeg$/.jpg/' <files> — Normalize JPEG extensions.
rename 's/\.jpeg$/.jpg/' *.jpegrename 's/$/.bak/' <files> — Add .bak extension to all files.
rename 's/$/.bak/' *.confrename 's/\.bak$//' <files> — Remove .bak extension from all files.
rename 's/\.bak$//' *.bakNumbering & Padding
rename 's/(\d+)/sprintf("%03d",$1)/e' <files> — Zero-pad numbers in filenames to 3 digits.
rename 's/(\d+)/sprintf("%03d",$1)/e' file*.txtrename -N 1 's/^/sprintf("%03d_",$N++)/e' <files> — Add sequential numbers as prefix.
rename -N 1 's/^/sprintf("%03d_",$N++)/e' *.jpgrename 's/(\d+)/$1+100/e' <files> — Offset numbers in filenames by 100.
rename 's/(\d+)/$1+100/e' track_*.mp3Common Patterns
rename 's/ /_/g' <files> — Replace spaces with underscores in filenames.
rename 's/ /_/g' *rename 's/[^a-zA-Z0-9._-]/_/g' <files> — Sanitize filenames: replace special chars with underscores.
rename 's/[^a-zA-Z0-9._-]/_/g' *rename 's/(.+)\.(.+)/$2.$1/' <files> — Swap filename and extension.
rename 's/(.+)\.(.+)/$2.$1/' *.txtrename 's/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/' <files> — Insert dashes in date-formatted filenames.
rename 's/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/' 20260315*.logrename 's/^/prefix_/' <files> — Add a prefix to all filenames.
rename 's/^/2026_/' *.csv Conclusion
The Perl rename is the most powerful batch-renaming tool around, because it brings the full expressive power of regular expressions – including case modifiers (\L, \u) and evaluated code via the /e flag for numbering and calculations. The one rule to remember: make sure which rename is installed, because the util-linux rename on RHEL/SUSE does not understand this Perl syntax and may do something entirely different. Run every operation with -n first, review the preview, and only then run it for real – a wrong expression can mangle many files at once. For simple single renames, mv is enough; for more complex logic beyond filenames, reach for a shell loop with mv and sed.
Further Reading
- Debian manpages: rename – official reference for the Perl rename
- Linux man page: rename(1) (util-linux) – manual page for the incompatible util-linux variant