LinuxCommandLibrary

perl-rename

Rename multiple files using Perl expressions

TLDR

View documentation for the original command

$ tldr -p common rename
copy

SYNOPSIS

rename [options] perlexpr [file ...]

PARAMETERS

-v
    --verbose
Prints the names of files successfully renamed.

-n
    --no-act
--dry-run
Shows what files would be renamed without actually performing the operation. Highly recommended for testing complex expressions.

-f
    --force
Overwrites existing files without prompting.

-i
    --interactive
Prompts before overwriting an existing file.

-p
    --path
Treats the entire path as the name to be renamed, not just the filename part.

-d
    --dirs
Renames directories as well as files.

-o
    --overwrite
Same as --force. Renames and overwrites existing files.

-k
    --keep-name
Keeps the original name if the expression would result in an empty string.

-s
    --subst
Indicates that the expression is a substitution (e.g., s/foo/bar/). This is the default if no other expression type is specified.

-e
    --expression
Specifies a Perl expression to be evaluated for each name. The expression should return the new name or undef to skip.

--
    Marks the end of options, allowing subsequent arguments to be treated as filenames even if they start with a hyphen.

--stdin
    Reads filenames from standard input, one per line.

DESCRIPTION

The perl-rename command, often simply referred to as rename on many Linux distributions, is an exceptionally powerful and flexible utility designed for batch renaming of files. It distinguishes itself from simpler renaming tools by leveraging the full power of Perl regular expressions for pattern matching and substitution. This allows users to perform highly sophisticated and complex renaming operations that would be difficult or impossible with basic shell commands.

For each filename provided as an argument, perl-rename applies a specified Perl expression (most commonly a s/pattern/replacement/ substitution) to derive the new filename. Its versatility makes it an indispensable tool for system administrators, developers, and power users who frequently need to organize, standardize, or transform large sets of files based on complex naming conventions.

CAVEATS

  • Confusion with `util-linux` rename: There are two common `rename` commands on Linux. This entry describes the Perl `rename` (often `/usr/bin/rename` or `/usr/bin/prename`), which uses Perl regular expressions. The `util-linux` `rename` (often `/bin/rename`) uses a simpler `sed`-like syntax or `mv` globbing. Always verify which `rename` command you are using (e.g., `which rename` or `man rename`).
  • No Undo: Once files are renamed, there is no built-in undo mechanism. Always use the `--no-act` (or `-n`) option to preview changes before committing.
  • Perl Regex Complexity: While powerful, Perl regular expressions can be complex. Errors in the expression can lead to unintended or incorrect renaming of files.

PERL EXPRESSION SYNTAX

The core of perl-rename's power lies in its ability to execute arbitrary Perl code, most commonly a substitution expression. The syntax for the Perl expression is typically s/pattern/replacement/flags, similar to `sed`.

  • pattern: A Perl regular expression to match against the current filename.
  • replacement: The string to replace the matched pattern with. This can include backreferences ($1, $2, etc.) to captured groups from the pattern.
  • flags: Optional modifiers, such as g (global replacement), i (case-insensitive), s (single line, makes `.` match newlines), x (extended, allows whitespace and comments in regex).

DEFAULT BEHAVIOR

If no specific expression type like -e is provided, rename assumes the first argument is a s/pattern/replacement/flags substitution.

HISTORY

The perl-rename command was originally written by Larry Wall, the creator of Perl, and released as part of the Perl distribution. Its design leverages the powerful regular expression capabilities of Perl, providing a highly flexible and efficient way to perform batch file renaming operations. It quickly became a standard utility in Unix-like operating systems due to its superior capabilities compared to simpler renaming methods.

SEE ALSO

mv(1), find(1), sed(1), grep(1), perl(1)

Copied to clipboard