The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


rename - renames multiple files


rename [-bfivnl] [-S backup-suffix] [-V {numbered,existing,simple}] [--backup] [--force] [--interactive] [--verbose] [--suffix=backup-suffix] [--version-control={numbered,existing,simple}] [--dry-run] [--just-print] [--link-only] [--help] [--version] perlexpr [files]...


Rename renames the filenames supplied according to the rule specified as the first argument. The argument is a Perl expression which is expected to modify the $_ string for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.

If a destination file is unwritable, the standard input is a tty, and the -f or --force option is not given, mv prompts the user for whether to overwrite the file. If the response does not begin with `y' or `Y', the file is skipped.


-b, --backup

Make backups of files that are about to be removed.

-f, --force

Remove existing destination files and never prompt the user.

-i, --interactive

Prompt whether to overwrite each destination file that already exists. If the response does not begin with `y' or `Y', the file is skipped.

-v, --verbose

Print the name of each file before renaming it.

-n, --just-print, --dry-run

Do everything but the actual renaming, insted just print the name of each file that would be renamed. When used together with --verbose, also print names of backups.

Link files to the new names instead of renaming them. This will keep the original files.


Print a usage message on standard output and exit.


Print version information on standard output then exit successfully.

-S, --suffix backup-suffix

The suffix used for making simple backup files can be set with the SIMPLE_BACKUP_SUFFIX environment variable, which can be overridden by this option. If neither of those is given, the default is `~', as it is in Emacs.

-V, --version-control {numbered,existing,simple}

The type of backups made can be set with the VERSION_CONTROL environment variable, which can be overridden by this option. If VERSION_CONTROL is not set and this option is not given, the default backup type is `existing'. The value of the VERSION_CONTROL environment variable and the argument to this option are like the GNU Emacs `version-control' variable; they also recognize synonyms that are more descriptive. The valid values are (unique abbreviations are accepted):

`t' or `numbered'

        Always make numbered backups.

`nil' or `existing'

        Make numbered backups of files that already
        have them, simple backups of the others.

`never' or `simple'

        Always make simple backups.


To rename all files matching *.bak to strip the extension, you might say

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

To translate uppercase names to lower, you'd use

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

More examples:

    rename 's/\.flip$/.flop/'       # rename *.flip to *.flop
    rename s/flip/flop/             # rename *flip* to *flop*
    rename 's/^s\.(.*)/$1.X/'       # switch sccs filenames around
    rename 's/$/.orig/ */*.[ch]'    # add .orig to source files in */
    rename 'y/A-Z/a-z/'             # lowercase all filenames in .
    rename 'y/A-Z/a-z/ if -B'       # same, but just binaries!
or even
    rename chop *~                  # restore all ~ backup files


Two environment variables are used, SIMPLE_BACKUP_SUFFIX and VERSION_CONTROL. See "OPTIONS".


mv(1) and perl(1)


If you give an invalid Perl expression you'll get a syntax error.


Peder Stray <>, original script from Larry Wall.