How to Change a Filename in Linux

In this tutorial, we will take you through the steps of changing a filename in Linux using the command line.

How to Change a File Name in Linux

There are a few different methods that you can use to change a filename on a Linux system. However, we will be focusing on using the mv and rename commands. Both of these commands are easy to use.

For this tutorial, we will be focusing purely on the terminal. However, for some Linux-based operating systems, you will have the option to use a GUI (Graphical User Interface) to alter file and folder names.

Table of Contents

Using the mv Command to Change a Filename

One of the easiest ways to change a filename is to use the mv command. This command is normally used for moving a file or directory but can also rename files and directories.

Below is an example of the syntax for rewriting a file using the mv command.

mv old_file_name new_file_name

Below is an example of renaming our index.html file to backend.php.

mv index.html backend.php

You can confirm that the filename has been updated using the ls command.

dev@pimylifeup:~/test$ mv index.html backend.php
dev@pimylifeup:~/test$ ls
backend.php

You can also do the same with a directory using the same logic. The example below will rename our www directory to html.

mv www html

Again, you can use the ls command to check that the directory’s name has been updated correctly.

dev@pimylifeup:~/test$ ls
www
dev@pimylifeup:~/test$ mv www html
dev@pimylifeup:~/test$ ls
html

Changing Multiple Filenames

You can use the mv command to change the name of multiple files and directories, but it will involve a bit of bash scripting. If you are unfamiliar with bash scripting, I recommend learning the basics, as it will make tasks within Linux a lot easier.

Our example will focus on mass renaming files with a specific file extension so they feature a new file extension. There is no reason why you cannot update the script to target different parts of a filename, but you may need to have a bit of bash knowledge to do so.

To start writing the bash script, create our script file with the nano file editor.

nano mass-rename.sh

Inside this file, add the following code. Afterward, save and exit by pressing CTRL + X, Y, then ENTER

#!/bin/bash

for f in *$1; do
    mv -- "$f" "${f%.$1}.$2"
done

Our script above will accept two parameters, $1 and $2. $1 is the file extension we wish to replace, and $2 is the new file extension.

The script internals are explained below.

  • We first write a shebang line that tells the operating system which interpreter to use. In our case, we want it to use bash.
  • The for loop will loop through each file ending with our $1 parameter. This file is stored in a variable f.
  • Our mv line will rename each file to have the new file extension ($2).

To run the script, you will need first to make it executable.

chmod +x mass-rename.sh

In our folder, we have some HTML files we wish to convert to PHP. So, we will run our script using the following command.

./mass-rename.sh html php

We can confirm that all our file extensions have been updated using the ls command.

dev@pimylifeup:~/test$ ls
contact.html  footer.html  header.html  home.html  mass-rename.sh
dev@pimylifeup:~/test$ ./mass-rename.sh html php
dev@pimylifeup:~/test$ ls
contact.php  footer.php  header.php  home.php  mass-rename.sh

There are other methods of using the mv command to change multiple files, such as using it with the find command. This option is a little more complicated than our bash script, which achieves the same result. If you like the idea of using find, I recommend looking into it.

Using the rename Command to Change a Filename

This method on changing filenames involves using the rename command, which typically is not preinstalled on most Linux operating systems. As you can tell from the command’s name, it is used to rename files and directories.

The rename command uses Perl expressions which might make it a little more daunting if you are a beginner. However, the command offers a wider range of features and will make some tasks easier. For this tutorial, we will only touch on the basics of using the command.

Installing the rename Command

The rename command will likely need to be installed before you can use it. The package name is slightly different depending on the operating system.

Ubuntu and Debian

Run the following command to install rename on Ubuntu or Debian.

sudo apt install rename

Fedora and CentOS

Using the following command will install rename on Fedora and CentOS.

sudo yum prename

Basic Examples of Using the rename Command

It is recommended that you understand Perl regular expressions to use this command effectively. If you are used to writing regex, you will find it easy to pick up quickly.

If you are not one hundred percent confident with the regular expression, you can use the -n option to output the files that will be changed but not actually perform the change. It is excellent for confirming that only the files you want renamed will be affected.

The syntax of the rename command is below. The “perlexpr” is the expression for renaming files, and “files” is the files you wish to rename.

rename [OPTIONS] perlexpr files

The examples we will be using will make use of the substitute expression. Below is an example of how you can structure the Perl expression (perlexr).

s/original/replacement/

You can replace the original and replacement string with regular expressions that can target a range of files. If you leave them as regular strings, the rename command will change any file or directory named original to replacement.

Adding an i at the end of the Perl expression will make it case insensitive. Also, adding a g will perform the substitution as often as needed, whereas without the g it will only perform the substitution once.

s/original/replacement/gi

Below is an example of how using g can impact renames. Our original file is called originalxoriginal.

dev@pimylifeup:~$ rename 's/original/replacement/' *
dev@pimylifeup:~$ ls
dev@pimylifeup:~$ replacementxoriginal

dev@pimylifeup:~$ rename 's/original/replacement/g' *
dev@pimylifeup:~$ ls
dev@pimylifeup:~$ replacementxreplacement

Below are several examples of how you can use this command to rename certain parts of a filename. You can do much more with the command than our examples show as it is quite powerful and versatile in how it renames files.

Rename a File

We will rename a single file in the first example by matching the full filename.

In the example below, we will rename our index.html file to home.php.

rename 's/index.html/home.php/' index.html

You can use the -v option to see feedback on each successful filename change. You can also use the ls command to confirm that rename command successfully updated the filename.

dev@pimylifeup:~$ rename -v 's/index.html/home.php/' index.html
index.html renamed as home.php
dev@pimylifeup:~$ ls
home.php

Change the File Extension

You can target a specific file extension you want to update. In this example, we will change our html files to php files.

The example below will run the Perl expression on all files ending with .html. By doing this, the command will update our file extension to .php.

rename 's/.html/.php/' *.html

For demonstration purposes, we added the verbose option to our command in the example below. As you can see, our rename command updated all the html files to have the php file extension. We can confirm everything worked correctly by using the ls command.

dev@pimylifeup:~$ rename -v 's/.html/.php/' *.html
body.html renamed as body.php
contact.html renamed as contact.php
footer.html renamed as footer.php
header.html renamed as header.php
index.html renamed as index.php
dev@pimylifeup:~$ ls
body.php  contact.php  footer.php  header.php  index.php

Replace Part of a Filename

This example will demonstrate how you can change part of a filename. If we wanted to remove the term backup from each file, we could write a command like the example below.

rename 's/backup//' *

As you can see in the example below, the above command removed “backup” from each of our filenames. Using regular expressions, you can further adjust the command to be more specific or less specific.

dev@pimylifeup:~$ rename -v 's/backup//' *
backupdb.sql renamed as db.sql
indexbackup.php renamed as index.php
dev@pimylifeup:~$ ls
db.sql  index.php

Change Case of a Filename

To change the case of the filenames, we will be making use of the y instead of s in our Perl expression. The y indicates we will be performing a translation. For example, the rename command will translate each character in our charset1 to the corresponding character in charset2.

y/charset1/charset2/

Using translation is the perfect way to change from lowercase to uppercase and vice versa. In our example below, we change every lowercase letter to an uppercase letter on all files within our current working directory.

rename 'y/a-z/A-Z/' *

As you can see in our output below, the command renamed our files to be in uppercase.

dev@pimylifeup:~$ rename -v 'y/a-z/A-Z/' *
db.sql renamed as DB.SQL
index.php renamed as INDEX.PHP

If you want to change to lowercase from uppercase, swap the Perl expression around, as shown in the example below.

rename 'y/A-Z/a-z/' *

The output below shows our filenames renamed to be lowercase instead of uppercase.

dev@pimylifeup:~$ rename -v 'y/A-Z/a-z/' *
DB.SQL renamed as db.sql
INDEX.PHP renamed as index.php

Conclusion

By now you should have a good understanding of how you can change a filename in Linux using the mv or rename command. These methods can be a lifesaver if you deal with altering files or directories often.

If you are new to Linux, I recommend you check out some of the many other Linux commands you can use to make changes to your system.

Please let us know if you notice a mistake or if an important topic needs to be added to this tutorial.

Leave a Reply

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