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

rename [options] 'expression' files

PARAMETERS

-v
    Verbose: Shows which files were successfully renamed.

-n
    No-action: Prints the names of the files that would be renamed without actually renaming them. This is a dry-run mode to test the expression.

-f
    Force: Overwrites existing files during the rename operation, if a file already has the target name.

-e 'command'
    Executes a perl command. Multiple commands can be specified.

DESCRIPTION

The `rename` command is a powerful tool used to rename multiple files based on Perl regular expressions. It iterates through a list of files and applies the provided Perl expression to each filename. The expression typically uses substitutions (s/old/new/) to modify the names. This command allows for complex and flexible renaming operations that would be difficult or impossible with standard `mv`.

CAVEATS

Care must be taken when using regular expressions, especially when overwriting existing files. It's advisable to use the `-n` option for a dry run before executing the actual rename.

EXAMPLES

Example 1: Replace all `.jpeg` extensions with `.jpg`:
`rename 's/\.jpeg$/\.jpg/' *.jpeg`

Example 2: Convert filenames to lowercase:
`rename 'y/A-Z/a-z/' *`

Example 3: Remove spaces from filenames:
`rename 's/ //g' *`

SEE ALSO

mv(1), find(1), sed(1)

Copied to clipboard