How to Rename a File in Linux

Files are the most important part of our digital life and renaming files is a common task for all technology users.

If you are a Linux user and you do not know how to rename a file in your Linux computer or want to learn how to rename a file in Linux properly then you are in right place.

Rename a File in Linux

There are two ways to complete any task on your Linux computer. If you are suitable with a Linux terminal you can rename a file using mv and rename utility. However there a way to rename the filename from the file manager.

Rename a File in Linux using mv Command

The basic use of the mv command is moving files from one directory to another directory. This command comes with any Linux distribution. You can find it on mac os or any Unix distribution.

The mv command stands for Move and it has its own purpose to use but we can still use it for renaming our files. While moving the file we can rename it using the mv command.

Structure of mv command

mv [OPTIONS] source destination

The mv word simply calls the moving command and options are completely optional. The source can be a file or directory and the destination also.

Using the mv command usually, we can rename a single file at a time, but we can move multiple files in a directory.

The source and destination must be an existing directory or file. When we rename to move a file in the destination we can assign a name for it.

In this case, we are going to rename a file name from test1.txt to text2.txt :

$ mv test1.txt test2.txt

At first, we have to open the terminal by pressing Ctrl+Alt+T at once and then change the directory where our file is located or we can also open our containing file via file manager and then right-click on an empty pace then select open in terminal.

Change test1.txt to your filename what you want to rename. Then change test2.txt to your new filename then hit enter your job will be done.

Rename Multiple File in Linux using mv Command

The mv command cannot rename multiple files at a time. But we can combine the mv command with other Utilities to do this job. However, this can only rename the file extension.

For batch rename, we are going to use lop. In this case, we are going to change all file extensions from .txt to .html inside the current directory.

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

Code breakdown

  1. The first line creates a for loop that lists all files that end with an .txt extension.
  2. The second line of code does the job. It changes all files extension to .html.
  3. Then the final line will break the loop.

Rename a File in Linux using mv Command

The rename command specially created for renaming files. This command is more advance than the mv command when it comes to renaming files.

This rename command requires some extra knowledge to wrap it and provide more benefits.  If you want to use the rename command-line utility. At first, you have to install it on your computer.

In this case, we are going to install the Perl rename module in our system.

Install rename utility on Ubuntu:

$ sudo apt install rename

Install rename utility on CentOS:

$ sudo yum install prename

Install rename utility on Arch Linux:

$ yay perl-rename

After install, we can use the rename command just like the mv command. The syntax for rename command shown below:

$ rename [OPTIONS] perlexpr files

The rename word simply calls the rename command and the option is completely optional for the user purpose. perlexpr is Perl syntax to rename files. You can learn more about Perl syntax on their official site.

The following example will change all files with the extension .txt to .html:

rename 's/.txt/.html/' \*.txt

You can use the -n flag to print names of files to be renamed, without renaming them.

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

The output will look something like this:

rename(file-90.txt, file-90.html)
rename(file-91.txt, file-91.html)
rename(file-92.txt, file-92.html)
rename(file-93.txt, file-93.html)
rename(file-94.txt, file-94.html)

By default, the command doesn’t overwrite existing files. Pass the -f flag to allow existing files to be overwritten:

rename -f 's/.txt/.html/' \*.txt

Rename Multiple File in Linux using File Manager

If you are not comfortable with the command-line interface you can try this one. This is the simplest way to rename files in Linux and you do not need any type of extra Linux Knowledge.

  1. To rename the file, open the File Manager and navigate to the according to file that you want to rename.
  2. Now Right-click on the file and select rename.
  3. After selecting rename your filename will be selected.
  4. Now you can press backspace and delete the existing name and type your new name and hit Enter.

Your job is done!

Conclusion

This is the complete guide on how to rename a file in Linux. In this complete guide, we have learned how to rename a file using mv and rename command or using file manager for GUI method.

If you found this article helpful share this with your friends.

Speak your mind!

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