Phone :  +370 (5) 204-1903
Email :  sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How to Rename File Linux? [Linux Rename File Command]

How to Rename File Linux? [Linux Rename File Command]

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 file Linux. It is an add-on for advanced terminal actions if you have administrative rights to the Linux terminal. 

Linux Tutorial Jan 07, 21 by Nisal N 10 min Read
How to Rename File Linux? [Linux Rename File Command]

List of content you will read in this article:

There are many types of commands that you can use to do this, like Linux mv or rename. However, the mv command is now outdated, and the Linux 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 have 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 rename files in a Linux terminal with various commands from the command-line prompt, whether you are an experienced Linux user or a beginner. 

mv command Linux (Linux Rename File)

Linux's mv command can perform two different tasks: copy and rename file Linux. 

  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 sending a file from directory A to directory B. 

The other usage of the mv command is to rename file Linux. 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 it ends up doing it. This widespread command is available for all Linux distributions and 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 following 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 should always be a single file, 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 the mv command

Use the following command to move or rename the file using the below syntax. A help command for mv 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. You can use this command to rename folder Linux (rename directory Linux). Then you can check for the new file's existence using the ls command and press Enter.

ls *.txt

How to rename multiple files in Linux?

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 will we 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 (bash rename) 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 ls command. If copy-paste does 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, It comes with a more straightforward way to rename multiple files in Linux using a different command. Let's discuss the rename command, which made this task simple.

Linux Rename Command

Rename command is widespread whenever it comes to renaming 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 per your distribution package manager's requirement. Here's how to set it up on your system:

Rename file Linux command.

  • Installing the rename utility on Ubuntu (Ubuntu rename file) and Debian

sudo apt-get install rename

  • Installing the rename utility on CentOS and Fedora

sudo yum install prename

  • Installing the rename utility on Arch Linux

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

  • Installing the rename utility 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 make the final change 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.

Rename file terminal examples

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 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.

How to change other parts of a filename with rename command Linux

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 prefixes such as "slang_."

ls sl*.c

We want to change the slang_ to 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_.

How to Delete a Part of a 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 file with nothing to make it delete.

ls *.c
rename 's/sl_//' *.c
ls *.c

The result will be as shown below:

It has deleted the sl_ part from each file name.

How to Search with a regular expression?

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 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 do Translations with 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

This article provides you with a complete insight into how the MV command (rename file Linux) works in Linux and other distributions. Also, where the mv command is difficult to use and implement for specific cases, the rename command comes to overcome the mv command's difficulty in renaming multiple files in Linux. We have described many instances with different command syntaxes where you can use the rename command Linux. You can comment in the below-given section if you have any more suggestions. If you're looking to host a website or run complex applications, buy linux vps can provide you with the power, flexibility, and security you need to meet your goals.

People Are Also Reading:

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