How to Rename Files and Directories on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

Renaming files and directories is one of the most basic tasks that every Linux user performs regularly using the GUI or command line tools.

In both of these tools, renaming a single file is pretty easy, but renaming multiple files can be trickier, especially for beginners who have just started using Linux.

In this article, you will learn how to rename single or multiple files using the mv and rename command line tools.

Tutorial Details

DescriptionRename Files and Directories in Linux
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesmv, find, rename
Internet RequiredNo

Renaming a Single File with the mv Command

The mv command is usually used to rename or move files from one location to another. It takes three arguments: one is the option, and the other two are the source and destination paths.


Here, you can specify multiple source files, but the destination should be mentioned, and that should be a path where the source files will be moved.

When you specify a single file, the destination can be a path or a filename, which will move your file to the specified filename (or rename your file).

For instance, the following command will change β€œfile.txt” in your current working directory to β€œmyfile.txtβ€œ.

$ mv file.txt myfile.txt

Renaming Multiple Files From the Command Line

The mv command has the ability to rename a single file, but that can be extended to multiple files by using the β€œfor” or β€œwhile” loop in Bash or piping it with the find command.

For example, the following lines of script use the β€œfor” loop to rename all the files with the β€œ.txt” extension to β€œ.pdf” in your current working directory.

for f in *.html; do
    mv -- "$f" "${f%.txt}.pdf"

Script breakdown:

  • The first line, β€œ#!/bin/bashβ€œ, is also known as β€œshebang” and is used to define the shell that will be used to execute the mentioned script.
  • The second line with the β€œfor” loop will iterate all the files with the β€œ.txt” extension.
  • The third line will replace each files with β€œ.txt” extension to β€œ.pdfβ€œ.
  • The last line ends the loop segment.

Let’s see how to rename multiple files by combining the mv and find commands.

So, first let’s take a look at the following command that will search for files with the β€œ.txt” extension and then replace them with β€œ.pdfβ€œ.

$ find . -depth -name "*.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%.txt}.pdf"' \;

The above command looks in the current working directory for all files with the β€œ.txt” extension and passes each one to the mv command using the β€œ-exec” option.

Then the mv command will replace the file with the β€œ.txt” extension to β€œ.pdf” within the β€œ{}” curly bracket.

Renaming Files with the rename Command

The rename command is another tool that uses the Perl expression (which can be trickier for beginners) to modify many aspects of files, including renaming.

Note that this tool is not shipped in all of the Linux distributions; however, you can check our recent article on the usage and installation of this rename tool in Linux.

Still, I’ve given you the following commands that will let you install it on your Linux system:

$ sudo apt install rename                                                                           #On Debian and Ubuntu
$ sudo dnf install prename                                                                        #On Red Hat and Fedora
$ sudo pacman -S rename                                                                        #On Arch and Manjaro

The syntax of the rename command is as follows:

$ rename [OPTION] perlexpr files

The rename command will rename the files based on the specified β€œperlexpr” regular expression.

For example, the following command will rename β€œfile1.txtβ€œ, β€œfile2.txtβ€œ, and β€œfile3.txt” to β€œmyfile1.txtβ€œ, β€œmyfile2.txtβ€œ, and β€œmyfile3.txt” in your current working directory.

$ rename -v 's/file/myfile/' *.txt

Or, you can use the following command to change the file extension from β€œ.txt” to β€œ.pdfβ€œ.

$ rename -v 's/.txt/.pdf/' *.txt

The following command will convert the filenames from lowercase to uppercase.

$ rename -v 'y/a-z/A-Z/' *.txt

The following command will convert the filenames from uppercase to lowercase.

$ rename -v 'y/A-Z/a-z/' *.TXT

The rest of the examples can be found in our detailed article, which is linked after the heading of this section.

Final Tips!

As you can see from all of the examples in this article, renaming a single file is far easier than renaming multiple files, which is why many beginners look for a GUI tool.

However, if you’re using the command line to operate on a remote server, this strategy will be useful. Still, if you want me to add a GUI tool to this article, then let me know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.