How to Rename a File or Directory on Linux

In this guide, you will be learning how to rename a file or directory on a Linux-based system.

linux rename file

There are a few different ways to rename a file on a Linux system, and we will be going into two of them.

Knowing how to rename a file on Linux using the terminal is essential to managing your device.

The primary method for moving a file is to use the mv command. It is simple to use and is excellent when you don’t need to perform any complex renaming.

The alternate method we will be exploring is to use the “rename” package. This package provides a powerful Perl-based renaming tool that we can use.

Using the mv Command to Rename a File on Linux

The mv command is the easiest and most commonly used way to rename a file on Linux-based systems.

While typically, the mv command is used to move files from one location to another, it can also be used in the same way to rename a file.

Below you can see the syntax for using the mv command within the command line. It is relatively simple to use as you don’t need to worry about the options parameter.

mv [OPTIONS] source destination

The two important things to consider here are the “source” and “destination” parameters for the mv command.

To rename a file, we can set “source” as the file that we want to rename. Then we can set the “destination” as the name we want to rename the file to.

Using this method, it is only possible to rename one file at a time. The mv command does not allow you to specify multiple “sources” and “destinations”.

Example of Renaming a File using mv

To show you how this works, we will give you an example of renaming a file using the mv command.

If we were to rename a file from “example1.txt” to “example2.txt“, we can use the following command.

mv example1.txt example2.txt

You can even rename the file when moving it from one directory to another. The two locations don’t have to be the same.

For example, if we wanted to rename a file or directory while renaming it, we can use a command like the one we have below.

You can see that we have used two different directories for both our source and our destination.

mv /home/pimylifeup/example1.txt /home/linuxguides/example2.txt

Using the rename Package to Rename Files on Linux

One of the most significant downsides of using the mv command to rename files is that it is only really useful for renaming one file at a time.

While there is a way to achieve a bit more control with the mv command by using loops, it is far easier to use a different program called “rename” or “prename“.

This program is written in Perl and allows us to perform more complicated renames by using regex“.

Installing the rename Package on your Linux System

The Perl rename package is not typically installed by default to your Linux system, so we will need to install it.

As the package manager differs depending on what distribution you are using, these commands will vary slightly.

Installing rename on Debian or Ubuntu

The apt package manager is used by various operating systems, two of the most popular ones being Debian and Ubuntu.

You can install the “rename” package to your system by using the command below.

sudo apt install rename

Installing rename on CentOS, Fedora or RHEL

CentOS, Fedora, and RHEL all use a different package manager to Debian-based systems.

For older versions of these operating systems, you will be making use of the yum package manager. Newer versions of the OS will be using the dnf package manager.

To the end-user, these package managers work the same, so installing the rename package is a straightforward process.

To install the rename package on your CentOS, Fedora, or RHEL system, run the following command.

sudo yum install prename

Syntax of the rename Command

The syntax for the rename command is very straightforward. The only complicated thing is that you will need some understanding of regular expressions.

Below you can see the syntax for the rename command.


While there are a few options that you can use alongside rename to control how you rename files, we will quickly go into a few of the most useful ones.

  • -v (--verbose) – Print the names for every file that was successfully renamed.
  • -n (--nono) – Don’t rename the files but print the files that match the expression.
  • -f (--force) – Overwrite files if the names end up matching when being renamed.

Before using this command to rename files, you will want to look up Perl regular expressions.

Example of Renaming Files with the rename Command

Now that you should have the Perl rename utility installed on your Linux command, let us put it to use.

We won’t be going t0o far into how to rename files on Linux using this tool, but we will show some basics.

Replacing File Extensions with the Rename Tool

For this example, we will replace the extension of all our “.txt” files to “.html“.

rename s/.txt/.html/ *.txt

Let us break down the Perl regular expression that we are using for our rename command.

The Perl Regular Expression

First, we are using the substitution operator (s//). This allows us to select text and provide a replacement easily.


Within this substitution syntax, we specify “.txt” as the pattern we are searching for. You can also use a regular expression here.

For the “REPLACEMENT” text, we set it to the extension we want our files renamed with. In our case, we will set this replacement text to “.html

The Find Files Pattern

Finally, we use a regular expression to select all files with the “.txt” extension within the current directory.


As the rename tool supports regular expressions, we use the * quantifier followed by the “.txt” extension.

Running the rename Tool

Let us now show you how the rename tool will scan and rename files on your Linux system.

To do this, we will use the “-n” option so that it outputs the changed filenames to the command-line.

rename -n s/.txt/.html/ *.txt

From this, you will get a result as we have shown below.

You can see that all of our files with the “.txt” extension were matched. You will also see the new name that was generated with the new “.html” extension.

rename(pimylifeup10.txt, pimylifeup10.html)
rename(pimylifeup11.txt, pimylifeup11.html)
rename(pimylifeup12.txt, pimylifeup12.html)
rename(pimylifeup13.txt, pimylifeup13.html)
rename(pimylifeup14.txt, pimylifeup14.html)
rename(pimylifeup15.txt, pimylifeup15.html)
rename(pimylifeup16.txt, pimylifeup16.html)


This guide will have shown you some of the ways that you can rename a file on a Linux system.

The easiest option is to use the “mv” command to give the file a new name. The downside of using this command to rename a file is that it only works on single files.

If you need to rename multiple files, using something like the Perl rename utility is a better option. This tool lets you use regular expressions to replace text and select files.

If you experience any issues with renaming a file on your Linux system or have a better solution, please leave a comment below.

Also, be sure to check out our other Linux tutorials and our guides for the Ubuntu operating system.

Leave a Reply

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