How To Change Multiple File Extensions From The Terminal

Hi geeks,

In this tutorial I will show you how to change multiple file extensions from the terminal. I have seen many  people googling to find a solution for this problem, so I thought to share this tutorial with you guys. You need to know a little bit bash script in order to understand everything in this article, but don’t be scared and go away because even if you don’t know anything about bash we are here to explain everything to you.

1. Open a new terminal and create the following directory in you desktop.

cd  /home/oltjano/Desktop
mkdir unixmen_tutorial

2.  cd to unixmen_tutorial and create the following files.

a.txt   b.txt  c.txt

3.  Ok guys it is time for some action. Run the following piece of code in the terminal and see what happens.

for i in *.txt; do echo $i; done

4. The following screenshot shows the result  that you should get in your terminal.

So what we are trying to do here is running a for loop and printing every filename with the .txt in the current directory. Ok, now run the following commands. It is used to strip the extension from a file.

a=a.txt
echo ${a/.txt}

5.  Do you see the following result?

6.  Ok, now run the following piece of code in your terminal. Have the file extensions changed?

for i in *.txt;  do mv "$i" "${i/.txt}".jpg; done