In this tutorial, we will take you through the steps of changing a filename in Linux using the command line.
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.
Below is an example of renaming our
index.html file to
You can confirm that the filename has been updated using the ls command.
You can also do the same with a directory using the same logic. The example below will rename our
www directory to
Again, you can use the ls command to check that the directory’s name has been updated correctly.
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.
Inside this file, add the following code. Afterward, save and exit by pressing CTRL + X, Y, then ENTER
Our script above will accept two parameters,
$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
$1parameter. This file is stored in a variable
- Our mv line will rename each file to have the new file extension (
To run the script, you will need first to make it executable.
In our folder, we have some HTML files we wish to convert to PHP. So, we will run our script using the following command.
We can confirm that all our file extensions have been updated using the ls command.
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.
Fedora and CentOS
Using the following command will install rename on Fedora and CentOS.
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.
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 (
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.
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.
Below is an example of how using
g can impact renames. Our original file is called
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
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.
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
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
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.
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.
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.
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
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.
As you can see in our output below, the command renamed our files to be in uppercase.
If you want to change to lowercase from uppercase, swap the Perl expression around, as shown in the example below.
The output below shows our filenames renamed to be lowercase instead of uppercase.
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.