en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How to rename files in Linux
How to rename files in Linux

07

Jan, 21

If you are a Linux user, you must have used terminal commands for various files and directory actions. One of the commonly used commands is to rename the file or directory. If you have administrative rights to the Linux terminal, it is an add-on for advanced terminal actions. 

There are many types of commands that you can use to do this, like mv or rename. However, the mv command is now out of date, and the rename command is getting popular. Apart from the command-line interface, Linux also offers the powerful CLI (Command Line Interface) that gives you various renaming commands. 

To leverage Linux's power, you should be having a strong knowledge of the Linux commands and how the files and directories work in Linux. Also, renaming a file, folder, or directory will impact the Linux hierarchy's directory chain.

This article will show you how to get through various commands and syntax to rename a file from the command-line prompt, whether you are an experienced Linux user or merely a beginner. 

mv command in Linux

Linux's mv command can perform two different tasks.

  1. Move a file 
  2. Rename a file

The mv command allows you to move files from one source location to a destination. Moving a file means that you can send a file from directory A to directory B. 

The other usage of the mv command is to rename a file. While that is true, usually, the mv command ends up doing both at the same time. It means that you can move an existing file to a new file location by defining it with a new name.

The mv command's designated job is not to rename the file but ends up doing it. This command is widespread and is available for all Linux distributions and other Unix-like operating systems.

Though there is nothing wrong with using the mv command, many administrators do not find it suitable for the purpose. 

Useful mv commands

mv --help

The above command will show the below details-

-f – will specify that no message is there before overwriting a file.

-i – identifies the warning messages before it overwrites or renames a file.

-u – This option will move a file only if it is new and if not present in the destination path or folder.

-v – it will specify what will be done by the command.

Below are the parameters for the command:

[SOURCE] – define the source path of the file

[DESTINATION] – specify the destination directory.

Rules to follow when using the mv command

You have to follow some rules while renaming any file:

  • The source always should be a single file or multiple files, or a directory. The destination path should always be a single file or directory.
  • If you are using multiple files for the source, the destination should be a directory to move to that directory quickly.
  • If you are using a file for the source and an existing directory for the destination, it will be moved to that destination directory.
  • You must provide a source and destination target for the working of the command.

Process for Renaming a Single File With mv Command

Use the following command to move or rename the file using the below syntax. There is a help command for mv that will provide syntax and different options.

Example 

You have to type mv following the old file name and the new file name on the command-line interface. This process is the same for all Linux and Unix distributions. 

mv oldfile.txt newfile.txt

The above command will rename the old file to the new file; press Enter after typing the above command. Then you can check for the new file's existence using the ls command and press Enter.

ls *.txt

Process of Renaming Multiple Files With the mv Command

Renaming multiple files with the mv command seems problematic as the initial designation wasn't for this. Suppose we have a directory with multiple files with different extensions, then how we will accommodate the mv command in moving all those files to the destination. 

With the below command, we will check the list of the existing files in a directory.

ls *.prog -l

Example 

We can use a bash script to rename all the files within the directory that will rename all the .prog files to .prg.

for f in *.prog; 

do mv -- "$f" "${f%.prog}.prg"

done

Now we will check if the above command has worked by using the below command. If copy paste will not work, type the command directly!

ls *.prg

Yes, the result shows that all the files with the .prog extension have changed to the .prg extension within the directory. Now we will analyze what the above bash code did.

  • The first line includes a loop that will iterate all files present within the directory with the .prog extension.
  • The second line includes the for-loop list, which will remove the .prog part from the filename and change it to the (.prg) extension.
  • The 'done' will end the for-loop.

This method seems to be a bit difficult, especially for a beginner. But yes, Linux comes with a more straightforward way to rename multiple files using a different command. Let's discuss the rename command, which made this task simple.

How to Implement Rename Command in Linux

Rename command is widespread whenever it comes to rename single or multiple files. It provides an easy way to do it, which is not possible with the mv command. However, you should have a good knowledge of using regular expressions. You can install the right version of the rename command as per your distribution package manager's requirement. Here's how to set it up on your system:

Commonly used rename commands.

  • Installing the rename on Ubuntu and Debian

sudo apt-get install rename

  • Installing the rename on CentOS and Fedora

sudo yum install prename

  • Installing the rename on Arch Linux

yay perl-rename ## or yaourt -S perl-rename

  • Installing the rename on Manjaro Linux

sudo pacman -Syu perl-rename

Syntax:

rename [OPTIONS] perlexpr files

Where,

Perlexpr is the given expression according to which you will rename the file.

-s: This option will rename the files ignoring the symbolic links if present.

-v: This option will specify what files are being renamed if there are any.

-n: This option will set the final change to be visible to the users.

-o: This option will prevent you from overwriting the existing files.

-V: This option will specify the version information and exit.

-help: This option will display the help message of the rename command and exit.

Example 

We will now rename all the .prog extension files.

ls *.prog

rename 's/.prog/.prg/' *.prog

ls *.pr*

Let us see what the rename command has done.

  • The first part is the command name that can be chosen as per your distribution.
  • The last part will replace the (.prog) to (.prg) extensions.
  • The middle part specifies the action that will be implemented on each filename. The 's' is for a substitute option, which will look for (.prog) in each filename and substitute it with (.prg).
  • The middle defines the regular expression.

Changing the Other Parts of the Filename With the Rename Command

Here we will see how to change the other part of the file name rather than its extension. Suppose we have a directory with several files having prefix as "slang_."

ls sl*.c

Now we want to change the "slang_" with "sl_." We will see how the below command will work.

rename 's/slang_/sl_' *.c

ls sl*.c

Replaces all the files starting with "slang_" with "sl_."

Deleting the Part of the Filename Using the Rename Command

This process will also work in the same manner as the above rename commands. But this time, we will replace the part of the will with nothing to make it delete.

ls *.c

rename 's/sl_//' *.c

ls *.c

The result will look like below:

It has deleted the "sl_" part from each file name.

How to Search with Grouping?

This is a different case that can be easily implemented with the rename command. Suppose you have two or more files with similar strings in the filename but not the same. In that case, the simple substitution method will not work. We have to work on the regular expression to get the desired output.

ls str*.c

rename 's/(stri|stra)ng/bang/' *.c

ls ban*.c

Now you can see the result of how changing the regular expression has worked.

How to do Translations with the Rename Command?

The rename command will allow you to perform translations on the filename. Here we use "y" in place of "s" to specify that we are not doing substitution but translation. 

  • Converting filenames to uppercase

rename 'y/a-z/A-Z/' *

  • Converting filenames to lowercase

rename 'y/A-Z/a-z/' *

  • Replacing the spaces in filenames with underscores

rename 'y/ /_/' *

Conclusion

The above section will provide you with insight into how the mv command works in Linux and other distributions. Also, where the mv command is difficult to use and implement for specific cases. The remove command comes to overcome the difficulty of the mv command to rename the multiple files. We have described many instances with different command syntax where you can use the rename command.

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment