Hacker News new | past | comments | ask | show | jobs | submit login

zmv is great for when doing mass renaming of files, in zshell. You can for example rename all .jade files to .pug with:

    autoload -U zmv # or put this line in your ~/.zshrc
    zmv -w 'src/**/*.jade' 'src/$1$2.pug'

You can do a dry run by using the `-n` flag too.



The same thing can be done in a kinda portable way with the pre/post substitution/expansion operators (at the moment don't recall their correct name):

  for a in **/*.jade; do echo mv "$a" "${a%jade}pug"; done
If I'm not wrong it should work in Bash and Ksh too (bar the double * expansion), this when I don't have an Emacs at hand.

In the first invocation I prefer to do a "dry run", if it looks good I proceed removing the echo.

P.S. Your example above should use double quote instead of single quote, otherwise the content is not expanded by Zsh

P.P.S. One recall the pre/post operator looking at the symbols on a US keyboard, the post follows the variable symbol...


I think the single quotes are correct, and are whats in my history. I think they're arguments to the zmv program, which itself does the expansion and $1 substitutions. Otherwise I think that $1 would be substituted once when you run it instead of for each file.

I've used zmv to also reorganize the directory structure of files too, something that seems easier to do with its glob/variable replace syntax.

Nice to have a cross platform option for usage in scripts though.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: