How To Change Multiple File Extensions From The Terminal

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.

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

  • LinuxSytesNet

    Save as


    import os
    from sys import argv
    for filenames in (Suffix for Suffix in os.listdir(‘.’) if Suffix.endswith(argv[1])):
    new_name, newname_extension = os.path.splitext(filenames)
    os.rename(filenames, new_name + argv[2])

    python .txt .jpg

    a.txt, b.txt, c.txt -> a.jpg, b.jpg, c.jpg

  • Aaron Caffrey

    Can you explain me the deletion of my post, where I wrote a python alternative for your bash script ?

  • 0sConstruction

    I have a question…
    How would I convert this concept, into stripping the spaces from a list of file-names and replacing them w/ “_” underscores? I downloaded a bunch of video-game walkthrough’s, and the names are so full of spaces; that it makes the auto-completion a real drag sometimes. But if there were a script I could write to recurssively comb through a directory or group of directories w/in a directory it would be epic!

  • Patrick Ruymaekers

    rename ‘s/ /_/g’ *

    this works good for me, replacing spaces with _

  • PePas

    The normal bash way to substitute would be: ${i//tobereplaced/replacement}
    This replaces all ‘tobereplaced’.
    The article is actually wrong. Using ${$i/.txt} will strip the first occurrence, so if the filename was “this .txt is perfectly ok.txt” after the command in the article it will be “this is perfectly ok.txt.jpg”.
    The command should be: mv “$i” “${i/%.txt/.jpg}”
    (The /% only replaces if .txt is at the very end of the string.)

  • this domain

    This design is spectacular! You obviously know how to keep a
    reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Great job. I really enjoyed what
    you had to say, and more than that, how you presented it.
    Too cool!

  • Jamison

    Saved as a favorite, I love your blog!

  • website

    I visited several sites except the audio feature for audio songs
    present at this web page is in fact wonderful.