LinuxCommandLibrary

rename

Rename multiple files using search and replace

TLDR

Replace from with to in the filenames of the specified files

$ rename 's/[from]/[to]/' [*.txt]
copy

Dry-run - display which changes would occur without performing them
$ rename -n 's/[from]/[to]/' [*.txt]
copy

Change the extension
$ rename 's/\.[old]$/\.[new]/' [*.txt]
copy

Change to lowercase (use -f in case-insensitive filesystems)
$ rename [[-f|--force]] 'y/A-Z/a-z/' [*.txt]
copy

Capitalize first letter of every word in the name
$ rename [[-f|--force]] 's/\b(\w)/\U$1/g' [*.txt]
copy

Replace spaces with underscores
$ rename 's/\s+/_/g' [*.txt]
copy

SYNOPSIS

rename [options] 's/search_pattern/replace_pattern/[flags]' [file...]
Example: rename 's/\.txt$/\.bak/' *.txt

PARAMETERS

-v, --verbose
    Displays the old and new names of each file being renamed, providing feedback on the operation.

-n, --no-act
    Performs a 'dry run'. It shows what files would be renamed without actually changing anything, crucial for testing complex regular expressions.

-f, --force
    Overwrites existing destination files without prompting.

-i, --interactive
    Prompts before overwriting an existing destination file, allowing the user to confirm or skip.

-o, --no-overwrite
    Do not overwrite existing files; if a target name already exists, the file is not renamed.

-k, --keep-structure
    Retain the directory structure when renaming files within subdirectories (often used with find).

-d, --directories
    Include directory names in the renaming operation, not just files.

-e, --expr
    Allows specifying the Perl expression on the command line, useful for scripting.

--version
    Displays the version information of the rename utility.

--help
    Shows a concise help message with available options.

DESCRIPTION

rename is a powerful command-line utility in Linux used for batch renaming of files based on specified rules. Unlike the simpler mv command which renames one file at a time or moves it, rename can apply a transformation to multiple filenames concurrently. The most widely used version, often referred to as Perl rename (or sometimes prename), leverages Perl's regular expression capabilities to provide flexible and sophisticated renaming operations. This allows users to perform complex patterns of substitution, deletion, or insertion within filenames. For instance, one can easily change file extensions, add prefixes/suffixes, remove specific characters, or reformat names across hundreds or thousands of files in a single command. It's an indispensable tool for system administrators and users managing large collections of files.

CAVEATS

  • Version Ambiguity: There are two distinct versions of the rename command on Linux: the Perl-based rename (analyzed here) and a simpler util-linux version (e.g., rename oldname newname file...). Their syntax and capabilities are vastly different. Always verify which version is installed on your system (e.g., man rename or rename --version).
  • Irreversible Operations: Renaming files is a destructive operation with no built-in 'undo'. Always use the --no-act or -n option first to test your renaming logic, especially with complex regular expressions.
  • Regular Expression Complexity: Incorrectly formulated regular expressions can lead to unintended or partial renames, or affect more files than intended.
  • Permissions: Ensure you have write permissions in the directories where files are being renamed.
  • Special Characters: Be cautious when dealing with filenames containing special characters (spaces, hyphens, etc.) as they may require proper escaping or quoting in shell commands.

REGULAR EXPRESSION SYNTAX (PERL)

The core of Perl rename's power lies in its use of Perl regular expressions, typically in the format 's/search_pattern/replace_pattern/[flags]'.

  • search_pattern: The regular expression to find within the filenames.
  • replace_pattern: The string that will replace the matched search_pattern. Backreferences (e.g., $1, $2) from captured groups in the search_pattern can be used here.
  • flags: Optional modifiers like g (global replacement, replace all occurrences), i (case-insensitive), s (single line mode), x (extended regex format, ignore whitespace for readability).

COMMON USAGE PATTERNS

  • Changing file extensions: rename 's/\.jpeg$/\.jpg/' *.jpeg
  • Adding prefixes/suffixes: rename 's/^/new_/' * (prefix); rename 's/$/_old/' * (suffix)
  • Removing parts of filenames: rename 's/ - Copy//' *
  • Converting spaces to underscores: rename 's/ /_/g' *

HISTORY

The rename command has a bifurcated history on Linux systems. The widely popular and powerful version, which is the focus of this analysis, is a Perl script that leverages Perl's strong regular expression engine. This version is often found pre-installed or easily installable on most distributions (e.g., Debian/Ubuntu's perl-rename package). Its development stemmed from the need for a more flexible batch renaming tool than mv could provide, especially for operations requiring pattern matching and substitution. Concurrently, a simpler rename utility is part of the util-linux project, commonly found on Red Hat-based systems (like Fedora, CentOS). This version is limited to a simple oldname newname transformation for a single file or a global string replace for multiple files, lacking the full power of regex. Due to its superior flexibility and regex capabilities, the Perl version has become the de-facto rename for advanced batch operations, making it an essential tool for scripting and file management.

SEE ALSO

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

Copied to clipboard