LinuxCommandLibrary

zmv

Batch rename files using zsh patterns

TLDR

Rename files lowercase to uppercase

$ zmv '(*)' '${(U)1}'
copy
Add prefix to all files
$ zmv '(*)' 'prefix_$1'
copy
Change file extension
$ zmv '(*).txt' '$1.md'
copy
Rename with sequential numbers
$ zmv -n '*.jpg' 'photo_$((++n)).jpg'
copy
Dry run (show what would happen)
$ zmv -n '(*)' 'new_$1'
copy
Move files to subdirectory
$ zmv '(*.txt)' 'subdir/$1'
copy

SYNOPSIS

zmv [-finqQsvwW] [-C|-L|-M|-p program] [--] srcpat dest

DESCRIPTION

zmv is a Zsh builtin for batch renaming files using pattern matching and replacement. It's more powerful than basic mv for complex renaming operations.
The source pattern uses Zsh extended globbing. Parenthesized groups are captured and available as $1, $2, etc. in the destination pattern. Zsh parameter expansion modifiers can transform captured text.
Common modifiers: ${(U)1} uppercase, ${(L)1} lowercase, ${(C)1} capitalize, ${1:t} tail (basename), ${1:h} head (directory).
zmv must be loaded first: autoload -U zmv

PARAMETERS

-n

Dry run, show what would be done
-f
Force overwrite without prompting
-i
Interactive, prompt before overwriting
-q
Quiet, don't report errors
-Q
Force bare glob qualifiers on pattern
-s
Symbolic link instead of move
-v
Verbose, print each command
-w
Pick out wildcard parts of pattern
-W
Same as -w but with pattern replacement
-C
Copy instead of move
-L
Hard link instead of move
-M
Move (default)
-p program
Use specified program instead of mv

CAVEATS

zmv is Zsh-specific. It won't work in bash or other shells.
Always use -n (dry run) first to verify the operation before executing.
Patterns use Zsh globbing, which differs from regular expressions. Enable extendedglob (**setopt extendedglob**) for full pattern support.

SEE ALSO

mv(1), rename(1), mmv(1), zsh(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community