LinuxCommandLibrary

prename

Rename multiple files using Perl expressions

TLDR

Rename files using a Perl Common Regular Expression (substitute 'foo' with 'bar' wherever found)

$ rename ['s/foo/bar/'] [*]
copy

Dry-run - display which renames would occur without performing them
$ rename [[-n|--nono]] ['s/foo/bar/'] [*]
copy

Force renaming even if the operation would remove existing destination files
$ rename [[-f|--force]] ['s/foo/bar/'] [*]
copy

Convert filenames to lower case (use -f in case-insensitive filesystems to prevent "already exists" errors)
$ rename 'y/A-Z/a-z/' [*]
copy

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

SYNOPSIS

prename 's/OLD/NEW/' FILE...

PARAMETERS

's/OLD/NEW/'
    The Perl regular expression to apply. 's' indicates a substitution. 'OLD' is the pattern to search for, and 'NEW' is the replacement string.

FILE...
    One or more filenames to rename.

DESCRIPTION

The prename command, often a symbolic link to rename, allows you to modify the filenames based on a regular expression. While it's primarily used for renaming files, it can also indirectly affect timestamps if the modification of the filename is considered a metadata change.

It uses Perl regular expressions for powerful pattern matching and replacement. This makes it highly flexible for complex renaming tasks, like bulk renaming files based on specific criteria (e.g., changing file extensions, adding prefixes, replacing characters). The prename command operates by iterating through a list of files and applying the specified Perl expression to each filename. If the expression modifies the filename, the file is renamed accordingly. While prename doesn't have direct options to modify ownerships or timestamps, it's important to understand that renaming is a metadata modification and might trigger timestamp updates in some file systems. It relies on Perl's s/// (substitution) operator for the actual renaming operation.

CAVEATS

Care must be taken when constructing the Perl regular expression. Incorrect expressions can lead to unintended renaming of files. It is recommended to test the expression on a small subset of files before applying it to a larger set. Renaming files can impact running applications or scripts that rely on the original filenames.

EXAMPLES

To rename all '.txt' files to '.log' files in the current directory:

prename 's/\.txt$/\.log/' *.txt

To add a prefix 'new_' to all files in the current directory:

prename 's/^/new_/' *

SEE ALSO

rename(1), find(1)

Copied to clipboard