LinuxCommandLibrary

rnm

Rename multiple files using patterns

TLDR

Replace a search string with a replacement string in filenames

$ rnm -ss [old] -rs [new] [path/to/directory]
copy

Use a fixed (literal) search and replace string instead of regex
$ rnm -ssf [old] -rs [new] [path/to/files]
copy

Add an auto-incremented index to filenames starting from 1
$ rnm -i 1 -inc 1 -rs [_] [path/to/files]
copy

Rename files using a list of new names from a text file
$ rnm -ns/f [path/to/names.txt] [path/to/files]
copy

Rename only files (ignoring directories and links)
$ rnm -fo -ss [pattern] -rs [replacement] [path/to/files]
copy

Sort input files by modification time before renaming
$ rnm -s/mt -ss [pattern] -rs [replacement] [path/to/files]
copy

Run a simulation without making actual changes
$ rnm -sim -ss [pattern] -rs [replacement] [path/to/files]
copy

Undo the last renaming operation
$ rnm -u
copy

SYNOPSIS

rnm [OPTIONS] [MODE_ARGS] PATH...

Common invocation patterns:
rnm [OPTIONS] --regex <PATTERN> <REPLACEMENT> PATH...
rnm [OPTIONS] --prefix <PREFIX> PATH...
rnm [OPTIONS] --to-lower PATH...

PARAMETERS

-n, --dry-run
    Shows what would be done without actually making any changes to files or directories. Highly recommended for safety.

-r, --regex <PATTERN> <REPLACEMENT>
    Rename using regular expression substitution. The first argument is the regex pattern to search for, the second is the replacement string.

-g, --glob <PATTERN> <REPLACEMENT>
    Rename using glob pattern matching for the search and replace.

-p, --prefix <PREFIX>
    Adds the specified <PREFIX> to the beginning of each name.

-s, --suffix <SUFFIX>
    Adds the specified <SUFFIX> to the end of each name.

-x, --remove-prefix <PREFIX>
    Removes the specified <PREFIX> from the beginning of names.

-y, --remove-suffix <SUFFIX>
    Removes the specified <SUFFIX> from the end of names.

-l, --to-lower
    Converts all characters in names to lowercase.

-u, --to-upper
    Converts all characters in names to uppercase.

-t, --to-title
    Converts names to title case (first letter of each word capitalized).

-c, --to-camel
    Converts names to camel case.

-k, --to-snake
    Converts names to snake case.

-m, --max-depth <DEPTH>
    Descends at most <DEPTH> levels below the starting directory. A value of 0 means only the starting directory itself.

-d, --dir-only
    Only operate on directories, ignoring files.

-f, --file-only
    Only operate on files, ignoring directories.

-i, --interactive
    Prompts for confirmation before each rename operation.

--force
    Forces renaming, overwriting existing files without prompt (use with extreme caution).

-v, --verbose
    Displays more detailed output during the operation.

DESCRIPTION

The rnm command, short for Recursive Name Manipulator, is a robust and flexible command-line utility designed for performing complex batch renaming operations on files and directories. Unlike simpler renaming tools, rnm supports a wide array of transformation methods, including regular expression substitutions, glob pattern matching, adding or removing prefixes and suffixes, changing case (to upper, lower, title case), and more. It can process files recursively through directories, making it ideal for organizing large collections of files. Key features include a crucial dry-run mode (-n or --dry-run) to preview changes before execution, various matching and replacement strategies, and the ability to operate on standard input, making it highly scriptable. rnm aims to provide a safer and more powerful alternative to scripting complex find and mv combinations for renaming tasks.

CAVEATS

rnm is not a standard utility included with most Linux distributions by default. It typically needs to be installed separately (e.g., via a package manager like Cargo for Rust-based versions). Due to its powerful recursive renaming capabilities, extreme caution is advised. Always use the --dry-run (-n) option first to preview changes before committing to any actual file modifications, especially when dealing with large numbers of files or complex regex patterns. Incorrect usage can lead to data loss or unrecoverable file name changes.

INSTALLATION

As rnm is not a standard command, it usually needs to be installed. For example, if it's the Rust-based version (a common implementation), it can often be installed via Cargo (Rust's package manager) using: cargo install rnm. Alternatively, pre-compiled binaries might be available on its GitHub releases page or via specific distribution's unofficial repositories. Consult the project's official documentation for the most accurate installation instructions.

USAGE EXAMPLES

Rename all '.txt' files to '.md' recursively in the current directory and subdirectories:
rnm -r '\.txt$' '.md' . -n

Add 'old_' prefix to all files/directories in the current directory (non-recursive):
rnm -p 'old_' * -n

Convert all file names within the 'docs' directory (and its subdirectories) to lowercase:
rnm -l docs/ -n

Remember to remove -n (--dry-run) for actual execution!

HISTORY

The rnm (Recursive Name Manipulator) tool is a relatively modern utility, often implemented in languages like Rust, developed to address the limitations of traditional renaming tools and intricate scripting approaches for batch renaming. Its development focus has been on providing a robust, performant, and feature-rich solution that can handle complex renaming scenarios with a clear, declarative syntax. It aims to reduce the boilerplate often associated with manual shell scripting for such tasks, offering a more intuitive and powerful recursive renaming capability than that provided by combining standard Unix utilities.

SEE ALSO

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

Copied to clipboard