LinuxCommandLibrary

zmv

Rename multiple files easily

TLDR

Move files using a regular expression-like pattern

$ zmv '[(*).log]' '[$1.txt]'
copy

Preview the result of a move, without making any actual changes
$ zmv -n '[(*).log]' '[$1.txt]'
copy

Interactively move files, with a prompt before every change
$ zmv -i '[(*).log]' '[$1.txt]'
copy

Verbosely print each action as it's being executed
$ zmv -v '[(*).log]' '[$1.txt]'
copy

SYNOPSIS

zmv [options]

PARAMETERS

-n
    Dry-run mode. Prints the renames that would be performed but does not actually execute them.

-v
    Verbose mode. Prints each rename as it is performed.

-w
    Overwrite existing files. Without this flag, zmv will refuse to overwrite existing files.

-f
    Force overwrite. Implies -w and disables safety checks regarding existing files.


    A shell pattern specifying the files to be renamed. Capturing groups can be defined within the pattern to be used in the replacement.


    A Perl expression that transforms the matched filename into the new filename. Uses $1, $2, etc. to refer to capturing groups defined in the pattern.

DESCRIPTION

zmv provides a powerful way to rename multiple files on a Unix-like system using shell patterns and Perl expressions. It allows you to specify a pattern that matches the files you want to rename, and then a Perl expression that transforms the matched filenames into the new names.
zmv offers several features to ensure safety, including dry-run mode, which previews the renames without actually performing them. It's especially useful when renaming large numbers of files, or when complex renaming logic is needed. The command is a zsh built-in, requiring no external dependencies beyond the shell itself. Zmv's flexible syntax and dry-run capabilities make it a go-to tool for advanced file management tasks.

CAVEATS

Care must be taken when constructing the Perl replacement expression to avoid errors. Always use the '-n' option for a dry run before performing the actual rename to ensure the command behaves as expected.

EXAMPLES

Rename all *.txt files to *.text: zmv '*.txt' '$f:s/.txt/.text/'
Rename files by adding prefix: zmv '(*)' 'new_$1'

HISTORY

zmv is a built-in command within the Zsh shell. It was developed to address the limitations of simpler renaming tools by providing a more flexible and powerful way to batch rename files using regular expressions. Its integration directly into the shell improves performance and simplifies usage.

SEE ALSO

rename(1), mv(1), find(1)

Copied to clipboard