How to Change a File Extension in Linux

In Windows, changing a file extension is as simple as right-clicking a file, clicking “Rename,” and changing .txt to .html. Because the Linux terminal does not have a graphical interface, the concept of “renaming” a file works entirely differently. In fact, Linux doesn’t even have a built-in rename command installed by default.

Instead, Linux treats changing a file name (or its extension) as moving the file. You are essentially taking the data from the old filename and moving it into a brand new filename. Therefore, you must use the mv (move) command.

How to Change the Extension of a Single File

If you only need to change a single configuration file from config.txt to config.bak to safely back it up, the mv command is the perfect tool.

  1. Open your terminal application.
  2. Use the cd command to navigate to the folder containing your file.
  3. The syntax requires three pieces of information: the mv command, the current name of the file, and the brand new name you want it to have.

mv old_filename.txt new_filename.html

For example, if you downloaded a script named installer.text and you need to change it to an executable bash script named installer.sh, you would type:

mv installer.text installer.sh

If you run the ls command to list the files in the directory, you will see that installer.text has vanished, completely replaced by installer.sh.

How to Change Extensions for Multiple Files (Bulk Rename)

The mv command is fantastic for a single file, but it is completely useless if you need to change 500 image files from .jpeg to .jpg. You cannot use the mv command on multiple files simultaneously.

To perform a bulk extension change, you must use a powerful utility called rename. (Note: Some minimalist Linux distributions do not include this tool by default. If you get a “command not found” error, type sudo apt install rename to download it).

The rename Syntax

The rename command uses a slightly complex syntax based on Perl regular expressions. It looks intimidating, but you only need to memorize one specific pattern.

The syntax looks like this: rename 's/old_extension/new_extension/' *.old_extension

Let’s break down exactly how to use this to fix our 500 image files.

  1. Navigate to the folder containing your 500 .jpeg images.
  2. Type the following command carefully:

rename 's/\.jpeg$/.jpg/' *.jpeg

Let’s explain what you just typed:

  • s/: This tells the utility to “Substitute” the first word for the second word.
  • \.jpeg$: This is the old extension you want to destroy. The backslash escapes the period, and the dollar sign ensures it only targets the very end of the file name.
  • /.jpg/: This is the brand new extension you want to apply.
  • *.jpeg: This final piece tells the utility to only run this substitution on files that currently end in .jpeg, ignoring any other files (like PDFs) in the folder.

Press Enter. The command will instantly rip through the folder, stripping the .jpeg extension off all 500 files and replacing it with a clean .jpg extension in a fraction of a second.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.