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.

https://youtu.be/_-9YsrkdcM8

Overview

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 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 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, 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.sh script.txt

The above command renames filename extension from sh to txt. Here, script.sh 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.sh script.txt
renamed 'script.sh' -> '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 script.sh new_script.txt
renamed 'script.sh' -> '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.

#!/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 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:~$ ./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 using the 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 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 Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.