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/' *.txt

rename 's/<old>/<new>/g' <files> — Replace all occurrences in filenames.

rename 's/-/_/g' *.html

rename -n 's/<old>/<new>/' <files> — Dry run: show what would be renamed without doing it.

rename -n 's/.jpeg/.jpg/' *.jpeg

rename -v 's/<old>/<new>/' <files> — Verbose: show each rename operation.

rename -v 's/IMG_/photo_/' *.jpg

rename -f 's/<old>/<new>/' <files> — Force: overwrite existing files.

rename -f 's/backup_//' *.sql

Case Changes

rename 'y/A-Z/a-z/' <files> — Convert filenames to lowercase.

rename 'y/A-Z/a-z/' *.JPG

rename 'y/a-z/A-Z/' <files> — Convert filenames to uppercase.

rename 'y/a-z/A-Z/' *.txt

rename '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/' *.txt

Extension Changes

rename 's/\.txt$/.md/' <files> — Change file extension from .txt to .md.

rename 's/\.txt$/.md/' *.txt

rename 's/\.jpeg$/.jpg/' <files> — Normalize JPEG extensions.

rename 's/\.jpeg$/.jpg/' *.jpeg

rename 's/$/.bak/' <files> — Add .bak extension to all files.

rename 's/$/.bak/' *.conf

rename 's/\.bak$//' <files> — Remove .bak extension from all files.

rename 's/\.bak$//' *.bak

Numbering & 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*.txt

rename -N 1 's/^/sprintf("%03d_",$N++)/e' <files> — Add sequential numbers as prefix.

rename -N 1 's/^/sprintf("%03d_",$N++)/e' *.jpg

rename 's/(\d+)/$1+100/e' <files> — Offset numbers in filenames by 100.

rename 's/(\d+)/$1+100/e' track_*.mp3

Common 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/' *.txt

rename '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*.log

rename '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

  • mv – move and rename individual files
  • find – find files and run actions on them
  • sed – transform text with a stream editor and regex