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.

$ mv [OPTION] [SOURCE] [DESTINATION]

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.

#!/bin/bash
for f in *.html; do
    mv -- "$f" "${f%.txt}.pdf"
done

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.