How to rename a file in Linux

This post will discuss the ins and outs of renaming files in Linux. Heads up! You may want to read our handy Most Used Linux Commands post before continuing further.

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 automation.sh. Here, ‘sh’ is an extension which 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 automation.sh. 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 file or a binary file 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 as well as through a bash/shell script.

mv Command:

First off! We can use the mv command to rename the filename extensions. The mv command can also be used to move the files from one location to another location in your system. So, it does two things, one is to move files and another is 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.sh script.txt

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

It is a good practice 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 what another extension.

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

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

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

Not only we have 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.

#!/bin/bash
for filename in *.txt;
do
    mv -v "$filename" "$(basename "$filename" .txt).docx"
done

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 basename command gives back the complete name of the file. You can skip the -v option in the second line but it is better to be used.

Lets see the output below:

user@server:~$ ./rename.sh
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 the use of basename command.

#!/bin/bash
for filename in *.txt; 
do
    mv -v "$filename" "${filename%.txt}.docx"
done

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
do
    mv -v "$filename" "${filename%.txt}.docx"
done

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

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 are not able to run the above command then you might not have rename command installed on your system. 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 as a result of this command execution. Like, it will tell you what filename has been renamed as what another filename. Let’s see this command with -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

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



About the Authors

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!

Support the Cause

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

Buy me a coffee



Leave a Reply

Your email address will not be published. Required fields are marked *