Home > File & Directory Management > rename

rename: Batch Rename Tool for Multiple Files

The `rename` command is used to change the names of multiple files at once using regular expressions. Unlike `mv`, it is very useful for batch modifying filenames based on patterns.

Overview

The `rename` command is a powerful tool used to change large numbers of filenames according to specific rules. This command can find and replace specific strings in filenames using Perl regular expression syntax like `s/old_pattern/new_pattern/`. For example, you can easily automate tasks such as changing all `.jpeg` extensions to `.jpg`, or replacing spaces in filenames with underscores (`_`).

Key Features

Key features of the `rename` command include:

  • Renames multiple files at once.
  • Provides powerful pattern substitution functionality based on regular expressions.
  • Optimized for batch operations, unlike the `mv` command which renames files one by one.
  • Supports a `dry run` mode to preview changes, helping prevent mistakes.

Differences between rename and mv

Both `rename` and `mv` are used to change file names, but they differ in functionality and purpose.

  • rename: Specialized in batch renaming multiple files based on patterns.
  • mv: Used to rename or move a single file or directory. Renaming multiple files requires a script.

Key Options

Commonly used `rename` command options are grouped by purpose.

1) Batch Renaming

2) Help

Generated command:

Try combining the commands.

Description:

`rename` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn the functionality of the `rename` command through various usage examples.

Change File Extension

rename 's/\.jpeg$/.jpg/' *.jpeg

Changes all `.jpeg` extensions to `.jpg` in the current directory.

Change Specific String in Filename

rename 's/^image_/photo_/' *.jpeg

Changes `image_` to `photo_` in all filenames starting with `image_`.

Replace Spaces with Underscores

rename 's/ /_/g' *

Replaces all spaces (` `) in filenames with underscores (`_`).

Convert Uppercase to Lowercase

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

Converts all uppercase letters in filenames to lowercase.

Preview Changes

rename -n 's/.html/.txt/' *.html

Previews the result of changing `.html` extensions to `.txt` without actually modifying the files.

Installation

The `rename` command is not included by default in all Linux distributions, so you may need to install it using the commands below. On some distributions, the package name might be different, such as `perl-rename` or `prename`.

Debian/Ubuntu

sudo apt update
sudo apt install rename

RHEL/CentOS/Fedora

sudo dnf install prename

Arch Linux

sudo pacman -S perl-rename

Tips & Cautions

Important considerations when using the `rename` command.

Tips

  • In regular expressions, a dot (`.`) matches any character. Therefore, when changing extensions, you must escape it with a backslash (`\`), for example, `\.jpeg`.
  • Since `rename` is a powerful tool, it's always recommended to use the `-n` option first to preview the results and prevent mistakes.
  • If filenames contain spaces, they should be enclosed in quotes (`"`) when passed as arguments.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands