prename
Rename multiple files using Perl expressions
TLDR
View documentation for the original command
SYNOPSIS
prename [options] perl_expression [file ...]
PARAMETERS
-n, --no-act, --dry-run
Performs a dry run, printing the names of the files that would be renamed and their new names without actually changing anything. This is highly recommended for testing your expression.
-v, --verbose
Prints the names of files successfully renamed.
-f, --force
Forces overwriting of existing target files. Use with caution.
-i, --interactive
Prompts before overwriting an existing target file.
-s, --symlinks
Operates on the targets of symbolic links, rather than the symlinks themselves.
-p, --path
Operates on the full path of each file, not just its basename. By default, only the basename is modified.
-k, --keep-last
With -p, this option prevents the final path component from being changed.
-u, --nocheck-target
Do not check for target existence before renaming. This can be slightly faster but less safe.
-h, --help
Displays a brief help message and exits.
-V, --version
Displays version information and exits.
DESCRIPTION
prename is a command-line utility commonly found on Unix-like systems, often a symbolic link or alias to the Perl rename command. Its primary function is to batch rename multiple files based on a specified Perl regular expression substitution.
Unlike simpler rename utilities that might only support sed-like string replacements, prename harnesses the full power of Perl's regular expression engine. This allows for highly flexible and complex renaming operations, such as changing file extensions, inserting or deleting characters, reordering parts of filenames, or converting case.
The command takes a Perl expression (typically in the s/original/replacement/[flags] format) and a list of files or directories as arguments. It processes each filename through the expression, calculates the new name, and then performs the rename operation. It's crucial to understand the regex syntax to use prename effectively. For safety, it's highly recommended to use the -n (dry-run) option first to see what changes will be made before actually executing the rename.
CAVEATS
The biggest caveat is the existence of multiple rename implementations on Linux systems. prename typically refers to the Perl rename utility, which is distinct from the util-linux rename (sometimes aliased as rename.ul). The Perl version uses Perl-compatible regular expressions (PCREs) for substitution (e.g., s/old/new/), while the util-linux version uses sed-like arguments (e.g., old new). Always verify which rename version prename points to on your system (e.g., readlink -f $(which prename)) to avoid unexpected behavior.
Always test your perl_expression with the -n (dry-run) option first, especially when dealing with complex regular expressions, to prevent accidental data loss or unwanted renames.
EXAMPLES
1. Change file extensions from .txt to .log:
prename 's/\.txt$/\.log/' *.txt
2. Remove spaces from filenames and replace with underscores:
prename 's/ /_/g' *
3. Convert filenames to lowercase:
prename 's/(.*)/\L$1/' *
4. Add a prefix "new_" to all .jpg files:
prename 's/(.*)/new_$1/' *.jpg
5. Reorder parts of a filename (e.g., "YYYY-MM-DD_title.txt" to "title_YYYY-MM-DD.txt"):
prename 's/^(\d{4}-\d{2}-\d{2})_(.*)\.txt$/$2_$1.txt/' *
Always remember to test these commands with -n (dry-run) first!
HISTORY
The Perl rename script, which prename often embodies, was written by Larry Wall, the creator of Perl. It emerged as a powerful solution for batch renaming files, leveraging Perl's strengths in text processing and regular expressions. Its flexibility quickly made it a preferred tool for complex renaming tasks over simpler, often less feature-rich rename implementations. Over time, it has become a standard utility in many Linux distributions, sometimes aliased as prename to differentiate it from other rename versions, or simply as the default rename command.


