How to change file extensions in unix?

Use the following shell script to change the file extensions in the current directory, for example from txt to jpeg.

changeFileExt.sh

#!/bin/sh
for f in $(ls *.$1 | cut -d "." -f1)
do
echo $f;
mv ${f}.$1 ${f}.$2
done

Execute the above shell script using the following command

sh ./changeFileExt.sh txt jpeg

The execution of above command will result in change of extension of all the (*.txt) files in the current directory to *.jpeg.

Search