How to rename a file in Linux

This post will discuss everything you need to know to rename a file in Linux. Heads up! You may want to read our handy Most Used Linux Commands post before continuing further.

Linux Shell Tutorial 4 How To Move and Rename Files


As you may know, a filename extension helps identify the type of file we are dealing with. For instance, a file may have its name as Here, ‘sh’ is an extension that stands for the shell. It means that this is a shell script that contains a set of commands.

Not all the files in Linux systems have their filename extensions mentioned. For example, you may come across a file with a name as automation instead of We can check the type of any file having no filename extension by running the following command:

file automation

Here, the file is the command name, and automation is the name of the file. This command will tell whether this file is a text, binary, or any other. Also, the file command will tell us if the file is empty.

See below the output from the file command for an empty file:

user@server:~$ file automation
automation: empty

Now let us see the output for a file with some text:

user@server:~$ file automation
automation: ASCII text

There are many ways to rename the filename extensions in Linux systems. Let’s see how we can rename filename extensions through a terminal and a bash/shell script.

The mv Command:

First off! We can use the mv command to rename the filename extensions. The mv command can also move the files from one location to another location in your system. So, it does two things: to move files and to rename files.

The syntax of the mv command is as follows:

mv [options] Source Destination

Let’s see some examples of how we can rename the filename extensions by running the mv commands in the terminal.

mv script.txt

The above command renames filename extension from sh to txt. Here, is the file’s name for which we want to change the extension.

It is good to use the -v option whenever we want to use the mv command. Using this option will prompt a message showing which extension has been renamed to another extension.

user@server:~$ mv -v script.txt
renamed '' -> 'script.txt'

Not only we can rename the filename extensions using the mv command, but we can also rename the file name:

user@server:~$ mv -v new_script.txt
renamed '' -> 'new_script.txt'

Not only have we changed the name of the file but also the filename extension. Initially, the name of the file was ‘script’ with a ‘sh’ extension. We have given it a new name as new_script with extension as txt.

Rename file Bash script

Now, let’s see how we can rename the filename extensions of multiple files through a shell script.

for filename in *.txt;
    mv -v "$filename" "$(basename "$filename" .txt).docx"

This script renames all the filename extensions from ‘txt’ to ‘docx’. In the first line, we are looping through all the text files available in the current directory.

In the second line, we are renaming all the ‘txt’ filename extensions from ‘txt’ to ‘docx’, where the basename command gives back the file’s complete name. You can skip the -v option in the second line, but it is better to be used.

Let’s see the output below:

user@server:~$ ./
renamed 'mydoc.txt' -> 'mydoc.docx'
renamed 'new_script.txt' -> 'new_script.docx'
renamed 'script.txt' -> 'script.docx'

Let’s see another script that does the same job as the above script without using the basename command.

for filename in *.txt; 
    mv -v "$filename" "${filename%.txt}.docx"

The above script renames all the ‘txt’ extensions of the current working directory. But how can we rename the extensions of another directory? Well, here is how you can do this.

for filename in ~/mv_test/*.txt
    mv -v "$filename" "${filename%.txt}.docx"

Here, ~/mv_test/ is the directory path we want to rename the extensions, while *.txt means all the text files present in this directory.

The rename Command:

 We can also use the rename command to rename a file and file extensions. According to the documentation, this command is used to rename multiple files.

The syntax of this command is as follows:

rename [options] 's/old_extension/new_extension/' files

Here, files mean those files for which we want to change the extensions.

Let’s see how we can use this command to rename the extensions.

rename 's/.txt/.docx/' *.txt

This command renames all the ‘txt’ extensions to ‘docx’ extensions. The rename command uses regular expressions, which can be super handy. The first ‘txt’ in this command is the old extension, and ‘docx’ will be the new extension of our files. Here *.txt at the end means we want to change the extensions for all the text files.

If you cannot run the above command, you might not have rename command installed on your system. However, you can run the following command to install it.

sudo apt install rename

Again, it is better to use the -v option as it will tell you what changes have been made due to this command execution. Like, it will tell you what filename has been renamed as what another filename. So, let’s see this command with the -v option.

user@server:~$ rename -v 's/.docx/.txt/' *.docx
mydoc.docx renamed as mydoc.txt
new_script.docx renamed as new_script.txt
script.docx renamed as script.txt

Wrapping up

In short, both the mv and rename commands can be used to rename a file. You can even combine these commands into a handy script that you can re-use. Lastly, the -v command is handy and can provide you with confirmation that it renamed the right file.

You may also be interested in

About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

Your email address will not be published.