[Switch] Linux rename command usage-Linux general technology-Linux programming and kernel information. The following is a detailed description. When I first learned about linux, I first thought of the rename Command for file rename. However, I tried to rename the file in windows and tried N times without responding. I searched for it online, many people know little about the rename Command. Some even say that there is no rename Command in linux. We recommend that you use the mv command. In view of this, man rename, a good study of its usage
Here, we will compare the rename and mv commands in renaming files. If you have any shortcomings, I hope you can correct them!
First, let's take a look at the mv command. In man mv, we can see that the introduction to the mv command is as follows:
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.
The rename command describes man rename as follows:
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. At the same time, it is worth noting 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
The following is an example:
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, the three wildcards can be used together. For other specific usage, you only need to constantly explore them.
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