LinuxCommandLibrary

rename.util

Rename multiple files

TLDR

Rename files using simple substitutions (substitute 'foo' with 'bar' wherever found)

$ rename [foo] [bar] [*]
copy

Dry-run - display which renames would occur without performing them
$ rename [[-vn|--verbose --no-act]] [foo] [bar] [*]
copy

Do not overwrite existing files
$ rename [[-o|--no-overwrite]] [foo] [bar] [*]
copy

Change file extensions
$ rename [.ext] [.bak] [*.ext]
copy

Prepend "foo" to all filenames in the current directory
$ rename [''] ['foo'] [*]
copy

Rename a group of increasingly numbered files zero-padding the numbers up to 3 digits
$ rename [foo] [foo00] [foo?] && rename [foo] [foo0] [foo??]
copy

SYNOPSIS

rename [OPTIONS] expression replacement file...

PARAMETERS

expression
    The literal string or substring to be found within the filenames.

replacement
    The literal string or substring that will replace the expression.

file...
    One or more paths to files that will be processed. Wildcards (e.g., *.txt) are commonly used here to specify multiple files.

-v, --verbose
    Displays detailed information about each renaming operation, showing the old and new filenames.

-s, --symlinks
    Prevents rename from dereferencing symbolic links. If a symlink is encountered, the symlink itself will be renamed, not its target.

-n, --no-act
    Performs a "dry run" by printing what actions would be taken without actually renaming any files. This is highly recommended for testing complex renaming operations.

-o, --old-file-names
    Allows filenames beginning with a hyphen (-) to be specified without being interpreted as options. This is useful when dealing with unusual filenames.

-V, --version
    Displays the version information of the rename utility and exits.

-h, --help
    Displays a help message with usage information and exits.

DESCRIPTION

The rename command, often provided as part of the util-linux package, is used for batch renaming of multiple files by replacing a specified string (expression) with another string (replacement) within their filenames. Unlike the mv command, which renames one file at a time or moves files, rename is designed for altering parts of filenames across a group of files. For instance, you can use it to change all occurrences of "old_" to "new_" in file names like "old_report.txt" to "new_report.txt". It operates by iterating through the list of provided files and, for each file, replaces the first occurrence of expression in its name with replacement. It's crucial to differentiate this rename from the Perl-based rename (often found on Debian/Ubuntu systems), which offers more powerful regular expression capabilities. The util-linux version performs a simple literal string replacement, not a regex match. Always use the -n (dry-run) option first to preview changes before committing to them.

CAVEATS

Literal String Replacement: This version of rename performs a literal string replacement. It does not support regular expressions like the Perl-based rename utility. If you need regex capabilities, ensure you are using the correct rename version (often prename or rename from perl-utils).
Overwrite Risk: rename can silently overwrite existing files if a new filename generated by the replacement already exists. Use the -n (dry-run) option and carefully review output to avoid unintended data loss.
First Occurrence Only: By default, rename replaces only the first occurrence of the expression within each filename. If expression appears multiple times and you wish to replace all, this command is not suitable for that.
Directory Handling: This command is primarily for renaming files. While you can specify paths containing directories (e.g., dir/oldfile), it will not rename the directories themselves; it only modifies the filenames within them.
Wildcard Expansion: The shell expands wildcards (e.g., *, ?) before rename sees them. Ensure your shell's wildcard expansion includes only the files you intend to rename.

EXAMPLE USAGE

To rename all files ending with .txt to end with .bak in the current directory, and see what would happen without actually doing it:

rename -n .txt .bak *.txt
To actually perform the above renaming operation:
rename .txt .bak *.txt
To replace "photo" with "image" in files like "my_photo_01.jpg" and "old_photo.png":
rename photo image my_photo_01.jpg old_photo.png

HISTORY

The rename command has a long history within the Linux ecosystem, particularly as part of the util-linux project, a collection of essential system utilities. Historically, there have been two predominant implementations of rename on Linux systems: the simpler, literal string replacement version (from util-linux) and a more powerful, regular-expression-based version (often written in Perl and distributed as perl-rename or prename). The util-linux version, which is the focus here, evolved to provide basic batch renaming capabilities by finding and replacing a specific substring within filenames. Its development aimed to offer a lightweight utility for common renaming tasks without the overhead or complexity of regular expressions, distinguishing it from its Perl counterpart which caters to more advanced pattern-matching requirements. Distributions like Red Hat Enterprise Linux and CentOS typically provide the util-linux rename as the default, while Debian and Ubuntu often default to the Perl version, leading to occasional confusion for users.

SEE ALSO

mv(1), find(1), perl(1)

Copied to clipboard