Show HN: Chext – a shell utility to change file extensions
github.com
github.com
autoload zmv
zmv '(*).baz' '$1.bar' mv $vflag $iflag $1 ${1%\.*}.$extI figured I would write something in the Unix philosophy to spare me (and maybe others) the trouble. It does exactly one thing, and it (hopefully) does it well. It was also fun to learn how to write a man page and do unit tests for a shell script.
mv *.bar *.baz
This is one place where Windows CMD works: ren *.bar *.baz
It can't work on Unix shells because wildcard expansion is done before the program (mv) is invoked. ls *.bar | while read f; do mv $f ${f%.bar}.baz; done for f in *.bar; do mv $f ${f%txt}baz; doneVery useful stuff. E.g. mv nginx.conf{,.old}
Before you know it, you have a script you've debugged and tested.
As to the portability of it, yes this is not portable as the syntax relies on substitution syntax supported by a limited number of Unix shells (bash and ksh spring to mind, others may support it too though).
Since the OP specifically mentioned:
mv foo.bar foo.baz
And you pointed out CMD's 'ren' command, the incantation I put forth seemed apropos. YMMV.UNIX makes it difficult because of the wildcard expansion. You can start writing some script, but there are gotchas galore such as correct escaping, IFS, shell portability, dry runs, interactivity, error handling and the list goes on. Having something that works as well as the simple Windows command is hard work, and before you know it end up publishing a github repository!
Dealing with one file is no problem. ...
The difficulties come when trying to work with
multiple files, and why you'd resort to the
command line.
Maybe we just have a different perception regarding these workflows. Not that either is superior or anything, just different. My background is one where the command line is not something I turn to, but instead one where I begin. UNIX makes it difficult because of the wildcard expansion.
In this specific situation, your point is spot on. I submit that in many other situations, the wildcard expansion employed by many Unix shells is very helpful. Not every tool is applicable for every situation and the specific case of renaming multiple files to those with different extensions highlights a shortcoming of many Unix shells. Perhaps an explanation is due to file extensions being of little importance to the vast majority of Unix programs. Having something that works as well as the
simple Windows command is hard work ...
Here, I will respectfully disagree and step away from this thread :-).You can dry-run first with `-n` flag if you're worried you didn't do it right, which is admittedly too possible. A bit more verbose, but a bit more powerful too.
^ http://manpages.ubuntu.com/manpages/trusty/man1/prename.1.ht...
http://manpages.ubuntu.com/manpages/trusty/man1/rename.ul.1....