Monday, October 14, 2013

How to change extension of multiple files (bash script)

The following is the script I use in Macbook (Mountain Lion). It should work on most *nix shell like bash, csh, ksh etc. However, I have tested it only in bash.

Script
            #!/bin/bash
            for f in *.$1; do
              [[ -f "$f" ]] && mv -v "$f" "${f%$1}$2"
              done
          

Save this file as "chext", make it executable chmod +x chext and add to your $PATH.

Now you can use it like this:

Usage
cd path-to-dir
chext oldextension newextension
Example
chext JPG jpg

Reference:

  1. http://forums.appleinsider.com/t/106498/how-to-change-the-file-extension-of-multiple-files/
  2. http://stackoverflow.com/questions/15829702/how-to-change-the-extension-of-multiple-files-using-bash-script/
  3. http://kb.iu.edu/data/abec.html
Related Posts Plugin for WordPress, Blogger...