File extensions help both operating systems and users distinguish between different file formats and understand the contents stored inside them. When you see a file with the ".txt" extension, you instantly know it contains text data. Similarly, ".exe" file is a Windows executable and ".sh" files are Linux shell scripts.

But what if you want to change these extensions for some reason? Perhaps you need to rename a text file to a Bash script. Simply writing the code in a text file won't do the job.

On Linux, changing file extensions is much easier than you might think.

Change File Extensions From the Terminal

Extensions are part of a file's name. And if you want to change the extension (or the name), you'd use the mv command. mv stands for "move" and is the standard command on Linux for moving and renaming files.

But why use mv for renaming files, you might wonder? It's because a rename operation is equivalent to a move operation in the same directory, just with a different name.

The basic syntax to rename files from the command line is:

        mv file.oldext file.newext
    

...where oldext and newext are the old and new extensions, respectively.

Consider you want to change a text file "myscript.txt" to a Bash script. Use the following command to do so:

        mv myscript.txt myscript.sh
    

You might have to prepend sudo to the mv commands depending on the file's owner.

Rename Multiple File Extensions at Once

Got an entire folder of files with inappropriate extensions? With a simple Bash for loop, you can change the extensions of multiple files all at once. For example, to change all TXT files in a folder to PDFs:

        for a in *.txt; do mv -- "$a" "${a%.txt}.pdf"; done
    

The aforementioned one-liner is a for loop that finds every file ending in ".txt". Then, it recursively performs a rename operation on all matched file names and replaces the ".txt" extension with ".pdf". The "done" at the end of the command indicates that the loop has finished. Simple!

How to Rename File Extensions Graphically

Linux has several desktop environments; some are very similar to the default Windows desktop, while others flaunt their uniqueness in each graphical element. But the user experience is more or less the same for all desktops. This means that renaming files on a Linux desktop is as easy as can be.

Open the file manager installed on your machine and select a file by highlighting it. Then, right-click and select Rename. In the field, you can modify the name of the file, including its extension. Proceed by changing the file extensions and then press Enter to finish.

changing file extension on ubuntu

Many Linux file managers also provide shortcuts you can use to save an extra click. For example, on GNOME (Nautilus file manager), you can press F2 while highlighting a file to quickly enter the rename function, eliminating the need to right-click. Similarly, on KDE Plasma (Dolphin) and XFCE (Thunar), you can hit the F2 key to change file extensions quickly.

Performing Basic File Management Operations on Linux

Renaming the extension of a file using the desktop interface is intuitive and easily done, but when you're dealing with multiple files at once, using the terminal is the optimal choice.

The Linux command line is a powerful tool for users who know how to use it. From day-to-day operations to highly-sophisticated system administration tasks, you can do it all with a terminal. And the best part, you don't even need a GUI to be able to use a Linux machine. That's primarily the reason why the terminal is still a part of the Linux ecosystem, in an age where graphical interfaces are the norm.