On Unix-like operating systems, the rename command renames multiple files, using regular expressions. It was written by Larry Wall, creator of the Perl programming language.

Description

rename renames the named files according to the regular expression perlexpr.

  • Description
  • Syntax
  • Perl expressions: a quick overview
  • Examples
  • Related commands
  • Linux commands help

If a specified file is not modified by the expression, it is not renamed. If no file names are given on the command line, file names are read via standard input.

Syntax

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

Options

Perl expressions: a quick overview

The perlexpr argument is a regular expression as used by the Perl programming language. Perl regular expressions is a complex and nuanced subject, but here is a brief overview:

Substitution

To substitute one expression for another, the form of perlexpr is:

s/expr1/expr2/[gi]

…where expr1 is an expression describing the string you want to replace, and expr2 is an expression describing the string you want to replace. For instance,

s/silly/foolish/

…would substitute the first occurrence of the string ‘silly’ with the string ‘foolish’.

To perform global substitution (that is, to substitute expr2 for expr1 as many times as expr1 occurs), add the modifier g at the end of the substitution expression. For instance:

s/silly/foolish/g

…would substitute every occurrence of ‘silly’ with ‘foolish’, no matter how many times it occurs.

To perform matching in a case-insensitive manner, add an i at the end of the substitution expression. For instance,

s/silly/foolish/i

…would substitute ‘SILLY’, ‘Silly’, or ‘siLLY’ with ‘foolish’.

The g and i modifiers may both be specified in the same expression, to perform case-insensitive global substitution, for example:

s/silly/foolish/gi

Metacharacters

A metacharacter is a character (or characters) with a special meaning. They are used in an expression to precisely define which strings should be matched and replaced.

These are some common metacharacters used in a Perl Expression:

Translation

Translation is similar to substitution. It can translate one string to another, character-for-character. Translation expressions are specified as follows:

y/charset1/charset2/

…where each character in the set charset1, in order, is to be translated into the corresponding character from the character set charset2. (These sets are like the character sets above, except you don’t need to put them in brackets.) For example, the translation expression:

y/abc/def/

…would translate every letter a into the letter d, every b into an e, etc.

This also works for charsets defined as ranges. For example:

y/a-z/A-Z/

…would translate every lowercase letter into its uppercase equivalent.

Examples

rename ’s/.jpeg$/.jpg/’ *

Rename any files with the extension “.jpeg” to have the extension “.jpg”.

find -type f -name ‘*.jpg’ | rename ’s/holiday/honeymoon/’

For all files with the extension “.jpg”, if they contain the string “holiday”, replace it with “honeymoon”. For instance, this command would rename the file “ourholiday001.jpg” to “ourhoneymoon001.jpg”.

This example also illustrates how to use the find command to send a list of files (-type f) with the extension .jpg (-name ‘*.jpg’) to rename via a pipe (|). rename then reads its file list from standard input.

rename ’s/.bak$//’ *.bak

Rename all files matching “*.bak” to strip the file name of its extension. For instance, this command would rename the file “project.bak” to “project”.

rename ‘y/A-Z/a-z/’ *

Rename files such that all uppercase letters are changed to their lowercase equivalents.

mv — Move files and directories from one location to another, and optionally rename them.perl — Interpreter for the Perl programming language.