Skip to content

[Linux] Use mv and rename command to rename a file

If we want to rename a file in Linux system, we can use mv and rename command.

Of course you can also change the name through graphic interface, but using command line is always much faster for me. In addition, the rename command can handle a large number of rename requirements at once.

In the past, I only used mv to rename the files... If there are multiple files that have to be renamed, I will simply write a shell script to deal with it. But today I learned the rename command and found that this is the function I want, so I recorded it here along with the mv command.


mv command

The mv command is abbreviation of the so-called move. The main function of this command is move files, but we can also rename files while moving the file path, so it has naturally become our command to rename files.

The use way is:

mv old_file new_file


Let's take an example. Suppose there are four files in my folder:


I can use mv command to rename the file.

mv 1.test 1.txt

Output:

We can find that we have renamed the original 1.test file to 1.txt.


rename command

The rename command is divided into two type.

  • rename.ul: C Programming Language
  • rename: Perl


The method of use is more complicated than mv, and there are basically three variables.

rename.ul replaced_string new_string match_file


Take the file I just made as an example, I can use the following command to rename all of them:

rename.ul .test .txt *.test

Output:


If you want to use perl version, you need to use the following command to install it:

sudo apt install rename


the rename command of perl version just only have two parameters:

  • match_pattern
  • match_files

The rename method is also very similar to the replacement in vim:

rename 's/.txt/.test/' *.txt

Output:


I personally find it very easy to use. How much time did I spend writing mv renamed shell script...

I really don't want to admit that this is a mistake I made when I was too young...

References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version