# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/rename/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Basic Usage

`rename 's/<old>/<new>/' <files>` — Replace first occurrence of a pattern in filenames.

```bash
rename 's/foo/bar/' *.txt
```

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

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

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

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

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

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

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

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

## Case Changes

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

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

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

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

`rename 's/(\w+)/\L$1/' <files>` — Lowercase entire filename using Perl modifiers.

```bash
rename 's/(\w+)/\L$1/' *
```

`rename 's/(\w)/\u$1/' <files>` — Capitalize first letter of filename.

```bash
rename 's/(\w)/\u$1/' *.txt
```

## Extension Changes

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

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

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

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

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

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

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

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

## Numbering & Padding

`rename 's/(\d+)/sprintf("%03d",$1)/e' <files>` — Zero-pad numbers in filenames to 3 digits.

```bash
rename 's/(\d+)/sprintf("%03d",$1)/e' file*.txt
```

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

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

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

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

## Common Patterns

`rename 's/ /_/g' <files>` — Replace spaces with underscores in filenames.

```bash
rename 's/ /_/g' *
```

`rename 's/[^a-zA-Z0-9._-]/_/g' <files>` — Sanitize filenames: replace special chars with underscores.

```bash
rename 's/[^a-zA-Z0-9._-]/_/g' *
```

`rename 's/(.+)\.(.+)/$2.$1/' <files>` — Swap filename and extension.

```bash
rename 's/(.+)\.(.+)/$2.$1/' *.txt
```

`rename 's/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/' <files>` — Insert dashes in date-formatted filenames.

```bash
rename 's/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/' 20260315*.log
```

`rename 's/^/prefix_/' <files>` — Add a prefix to all filenames.

```bash
rename 's/^/2026_/' *.csv
```

<!-- PROSE:outro -->
## 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](https://manpages.debian.org/bookworm/rename/rename.1p.en.html) – official reference for the Perl rename
- [Linux man page: rename(1) (util-linux)](https://man7.org/linux/man-pages/man1/rename.1.html) – manual page for the incompatible util-linux variant
<!-- PROSE:outro:end -->

## Related Commands

- [mv](https://www.jpkc.com/db/en/cheatsheets/files-text/mv/) – move and rename individual files
- [find](https://www.jpkc.com/db/en/cheatsheets/files-text/find/) – find files and run actions on them
- [sed](https://www.jpkc.com/db/en/cheatsheets/files-text/sed/) – transform text with a stream editor and regex

