brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: zmv -n previews a bulk rename in zsh before it touches a file

Shell
~/tips/tip-zmv-dry-run

zmv -n '(*).log' '$1.txt'

Preview a pattern-based bulk rename in zsh before running it.

Renaming a pile of files with a pattern is easy to get wrong, and mv has no undo. zsh has a helper that shows the plan first.

zmv ships with zsh but has to be loaded once. Then give it a pattern with a group in parentheses and a new name that uses $1:

autoload -U zmv
zmv -n '(*).log' '$1.txt'

The -n flag means no execution: it prints what would happen, for example mv -- one.log one.txt, and changes nothing. Happy with the list? Run the same command without -n, or with -v to see each rename as it happens.

A caveat: this is a zsh function, so it doesn’t exist in bash. Quote both patterns, otherwise the shell expands the glob before zmv sees it.

Further reading: zsh manual: zmv (zshcontrib)

More Shell tips zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS docker compose logs --tail 50 web # now at the top of a clean screen All Shell tips (78) All topics

Link