Linux rename command

Updated: 11/06/2021 by Computer Hope
rename command

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.


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

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.


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


-v, --verbose Verbose: print names of files successfully renamed.
-n, --no-act No Action: show what files would be renamed.
-f, --force Force: overwrite existing files.

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:


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


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


...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 often as expr1 occurs), add the modifier g at the end of the substitution expression. For instance:


...would substitute every occurrence of 'silly' with 'foolish', no matter how often it occurs.

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


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



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:

metacharacters meaning
^ Matches the beginning of a string.
$ Matches the end of a string.
. Matches any character, except a newline.
* Matches occurrences of the preceding character, or group of characters, zero or more times.
+ Matches occurrences of the preceding character, or group of characters, one or more times.
? Match occurrences of the preceding character, or group of characters, zero or one times.

If used after a repetition modifier, '?' specifies that the shortest possible match should be used. For instance, 'a{2,4}?' matches 'aa' even if 'aaa' and 'aaaa' would also match. See repetition modifiers, below.
| Alternation; behaves like a boolean 'OR'. For instance, 'butter|jelly' matches either butter or jelly.
(...) Grouping. For instance, '(eg|le)gs' matches either 'eggs' or 'legs'.
[...] A set of characters. For instance, '[abc]' matches 'a' or 'b' or 'c'. Character sets can be defined as:

[characters] Matches any one of the characters listed.
[x-y] Matches any in a range of characters between x and y, inclusive. For instance, '[c-e]' matches c, d, or e, and '[a-z]' matches any lowercase letter.
[^characters] Does not match characters; in other words, matches any character except those listed. Can also negate a character range; for instance, '[^a-d]' matches any character except a, b, c, or d.
[\-] Matches the hyphen character ("-").
[x-yX-Z] Multiple character ranges can be placed in a character set consecutively. For instance, '[a-zA-Z]' matches any letter, uppercase or lowercase.
{m[,[n]]} A repetition modifier that matches at least m and at most n of the preceding characters. For instance, 'a{2}' matches 'aa', 'a{2,4}' matches either 'aa', 'aaa', or 'aaaa', and 'b{2,}' matches two or more consecutive b characters.
\ Escapes a metacharacter so it is treated literally. For instance, '\+' matches a literal '+' (instead of the plus symbol having its special metacharacter meaning).
\t Matches a tab character.
\n Matches a newline character.
\r Matches a carriage return character.
\w Matches any single character classified as a "word" character (an alphanumeric character or an underscore '_').
\W Matches any single non-"word" character.
\s Matches any single whitespace character (space, tab, newline).
\S Matches any single non-whitespace character.
\d Matches any digit character. This switch is equivalent to the character set '[0-9]'
\D Matches any non-digit character.
\b A "zero-width" matching assertion that matches any "word boundary".
\B A "zero-width" matching assertion that matches any non-"word boundary".


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


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


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


...would translate every lowercase letter into its uppercase equivalent.


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.