Linux rename command usage learning to modify file names

Source: Internet
Author: User

Linux rename command usage learning to modify file names

How can I use a command to modify the file name? In Linux, you can use the rename Command and the mv command. Here we will share the usage of the Linux rename Command.

Some people say that there is no rename Command in Linux. We recommend that you use mv command.
Compare the rename command with the mv command to rename the file. You can choose to use it as you like.

Mv command:
Mv-move (rename) files

You can see that the mv command does have the rename function, but in actual applications, it can only rename a single file. The command is as follows:
Mv [path/] oldfilename [path/] newfilename

The mv command can only rename a single file. This is the fundamental difference between the mv command and the rename Command in renaming.

Rename command, in man rename description:
NAME
Rename-Rename files
SYNOPSIS
Rename from to file ....

DESCRIPTION

Rename will rename the specified files by replacing the first occurrence of from in their name by.

For example, given the files foo1,..., foo9, foo10,..., foo278, the commands

Rename foo foo0 foo?

Rename foo foo0 foo ??

Will turn them into foo001,..., foo009, foo010,..., foo278.

And

Rename. htm. html *. htm

Will fix the extension of your html files.

It can be seen that the rename Command is dedicated to file rename. According to the following example, rename can be used to rename a single file in batches.
Note that the rename command contains three parameters instead of the two parameters that many people think.

In the above example, the use of batch rename for two types of files is given. In fact, rename is used in combination with wildcards, and its function is more powerful than that shown in the above example.

Basic wildcards include the following:
? Can replace a single character
* Replace multiple characters

[Charset] can replace any single character in the charset set

Note:
If the folder contains these files foo1,..., foo9, foo10,..., foo278
Rename foo foo0 foo?

Then it will only rename the foo1 to foo9 file to foo01 to foo09, because? The wildcard character can only replace a single character. Therefore, the renamed file contains only four characters in length, and foo in the file name is replaced with foo0.

Continue to use
Rename foo foo0 foo ??

In this case, all the files from foo01 to foo99 in the folder are renamed to foo001 to foo099, while the names of foo100 and later files remain unchanged because of wildcards? So only the file with a length of five characters is renamed, And the foo in the file name is replaced with foo0.

If you continue to use
Rename foo foo0 foo *

All files from foo001 to foo278 are renamed to foo0001 to foo0278. Because wildcard * can replace multiple characters, all files starting with foo are renamed, replace foo with foo0 in the file name.

Let's take a look at the usage of the wildcard [charset], or continue in the folder mentioned above, and execute the following command
Rename foo0 foo foo0 [2] *

All files from foo0200 to foo0278 are renamed to foo200 to foo278, And the foo0 in the file name is replaced with foo.

In use, three wildcards can be used together.

In short, both the rename Command and the mv command can modify the file name. Pay attention to different parameters and usage when using them.

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.