How to Rename a File in Linux?

Learn via video courses
Topics Covered

How to Rename a File in Linux?

In Linux, you can easily rename a file using various methods. Two commonly used commands for renaming files are mv and rename. The mv command is used to move files, but it can also be used to rename files. The rename command is specifically designed for renaming multiple files at once.

Using the mv Command

Let's find out how to rename a file in Linux using mv command.

Syntax:

Renaming a Single File

Let's learn how to rename a file in Linux.

To rename a single file using the mv command, follow these below-mentioned steps:

Step 1:
Open a terminal and navigate to the directory where the file is located using the cd command. For example, if the file is in the home directory, you can use cd ~ to go to the home directory.

Step2:
Execute the mv command followed by the current file name and the desired new name:

Replace current_file_name with the actual name of the file you want to rename, and new_file_name with the desired new name for the file.

For example, if you want to rename a file called "folder1.txt" to "file1.txt," the command would be:

Step 3:
Press Enter to execute the command. The file will be renamed.

renaming single file

Renaming Multiple Files

mv command by its own can change a single file name only. However, if you combine it with other valid commands it can help you rename multiple files at once. Let's find out how.

In this method to rename multiple files at once, we will use a combination of mv command with find command. With find command we will select multiple files and with mv command we will rename them at once.

As an example, the above command explains the combination of the find command and the mv command. Now let's check it out practically.

Open a terminal and execute the below-mentioned command.

The above command shows the execution of renaming multiple files using mv command and find command as shown in the below image.

renaming multiple files

Using the Rename Command

The rename command is specifically designed for batch renaming multiple files at once. It uses regular expressions to match and replace parts of the file names.

The rename command is not present in Linux distros by default. An installation of the rename command is required to utilize this command for renaming files and folders.

To install rename use the below-mentioned command.

To rename multiple files using the rename command, follow the below-mentioned steps:

Step 1:
Open a terminal and navigate to the directory where the files are located using the cd command.

Step 2:
Execute the rename command followed by the regular expression pattern and the replacement pattern as shown below.

Replace pattern with the regular expression pattern that matches the part of the file names you want to replace, replacement with the desired replacement pattern, and files with the files you want to rename.

For example, if you have multiple files with the extension ".old" and you want to rename them to have the extension ".new", the command would be:

output rename command

Step 3:
Press Enter to execute the command. The files will be renamed. That's it; this is how to rename a file in Linux.

Conclusion

  • In this article, we have learned how to rename a file in Linux.
  • Renaming files in Linux can be easily accomplished using the mv command for renaming a single file or using combination of find command and mv command for renaming multiple files.
  • The rename command provides more flexibility for batch renaming multiple files using regular expressions.
  • Choose the method that suits your requirements and execute the appropriate command in the terminal as per your use.