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 [-s substituteflags] [-W globflags] [-options] pattern replacement

PARAMETERS

pattern
    A Zsh glob pattern matching the files to be renamed or moved. Parentheses (...) can be used to capture parts of the filename for later reference.

replacement
    The string specifying the new names for the files. Backreferences $1, $2, etc., correspond to the captured groups in the pattern.

-i
    Interactive mode. Prompt before each move/rename operation.

-f
    Force mode. Overwrite existing destination files without prompting.

-n
    No-execute mode (dry run). Show what would be done without actually performing any operations. Highly recommended for testing complex patterns.

-s substituteflags
    Pass specific flags to the substitution within the replacement string. For example, s:r for root name, s:e for extension.

-W globflags
    Pass specific flags to the globbing within the pattern. For example, W:D to include dot files, W:n to enable NULL_GLOB.

-Q
    Quote the arguments, treating them literally rather than as patterns or shell commands. Useful if patterns or replacements contain shell metacharacters.

-C
    Copy files instead of moving them. Equivalent to using cp instead of mv.

-L
    Create symbolic links instead of moving files. Equivalent to using ln -s.

-p
    Create parent directories as needed if the destination path requires them.

-o
    Pass the -f (force) option to the underlying mv or cp command, ensuring existing files are overwritten.

DESCRIPTION

zmv is a highly powerful and flexible function within the Zsh (Z Shell) environment, designed for renaming or moving multiple files using pattern matching. Unlike the standard mv command which renames one file at a time or uses simple wildcards, zmv leverages Zsh's advanced globbing features to perform complex, rule-based renames across many files simultaneously. It allows for the use of capturing groups in the source pattern, which can then be referenced in the destination name, enabling sophisticated transformations like reordering parts of filenames, adding prefixes/suffixes, or changing file extensions across entire directories. It is an invaluable tool for batch file operations when basic globbing isn't sufficient.

CAVEATS

zmv is a Zsh-specific function and is not available by default in other shells like Bash or Dash. It must typically be loaded using autoload -Uz zmv in your Zsh configuration. Due to its powerful pattern matching and batch operation capabilities, it can be destructive if not used carefully; always use the -n (dry run) option to preview changes before executing.

GLOBBING AND BACKREFERENCES

The true power of zmv lies in its use of Zsh's extended globbing. By enclosing parts of the pattern in parentheses (...), you create 'capturing groups'. These captured parts can then be referenced in the replacement string using variables like $1, $2, and so on, corresponding to the order of the capturing groups. For example, to rename files from 'IMG_1234.jpg' to 'photo_1234.jpg', you could use:
zmv 'IMG_(*).jpg' 'photo_$1.jpg'
This allows for highly flexible and complex renaming rules.

HISTORY

zmv originated as a powerful utility within the Zsh (Z Shell) environment to extend basic file renaming capabilities. It's not a standalone executable but rather a highly optimized Zsh function, often found in the zsh/files or zsh/zsh_misc module. Its development aimed to provide glob-based renaming that is more versatile and safer than simple shell loops for batch operations, incorporating Zsh's advanced glob qualifiers and parameter expansion features from its inception.

SEE ALSO

mv(1), rename(1), cp(1), ln(1), zsh(1)

Copied to clipboard