
Show HN: Chext – a shell utility to change file extensions - antognini
https://github.com/joe-antognini/chext
======
_paulc
In zsh:

    
    
      autoload zmv
      zmv '(*).baz' '$1.bar'

------
prashnts
How is it more convenient than `mv foo.bar foo.baz`?

~~~
rogerbinns
Now try with more than one file like this:

    
    
        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.

~~~
AdieuToLogic
While not quite as convenient, it's not that tough in bash:

    
    
      ls *.bar | while read f; do mv $f ${f%.bar}.baz; done

~~~
rogerbinns
Your version above has at least two bugs I see (will fall over with various
forms of white space in the filenames). And it would be nice to have
interactivity and verboseness (admittedly just flags to mv). It would also be
better to work with plain portable shell, rather than requiring bash, so
you'll need to include some basename calls. Also should it stop on the first
rename failure, or just keep ploughing on no matter what?

Before you know it, you have a script you've debugged and tested.

~~~
AdieuToLogic
The command I whipped up is, by definition, a one-liner meant for situations
where the file movement is known by the person wishing to perform it and done
interactively.

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.

~~~
rogerbinns
Dealing with one file is no problem. A regular rename on the command line or
gui file manager of choice works well. The difficulties come when trying to
work with multiple files, and why you'd resort to the command line. Amusingly,
the rename on Windows just works.

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!

~~~
AdieuToLogic

      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 :-).

------
crazydiamond
Try `rename`

[http://linux.die.net/man/1/rename](http://linux.die.net/man/1/rename)

If on OSX:

    
    
        brew install rename

