Linux rename command usage (suitable for modifying file names in large batches), linuxrename

Source: Internet
Author: User

Linux rename command usage (suitable for modifying file names in large batches), linuxrename

Rename command usage in Linux (suitable for modifying file names in large batches)

When watching laruence's Private food, I ran into the mv command, which allowed me to change the file name. There is also a command called rename. Laruence asked man to help with the query. The results show that the command can modify the file names in batches.

First, let's take a look at the instructions in man's help:

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.

Man's help gives us two examples. What does it mean? By Using rename foo foo0 foo like this command, you can modify regular file names such as foo1, foo2, and foo3 in batches. Rename has three parameters. The first parameter is the part to be modified, the second parameter is the part to be modified, and the third parameter is the file to be modified.

We still need to do the experiment.

We have created six files in the directory: foo1, foo2, foo11, foo22, foo111, and foo222.

[Root @ centos-01test] # ls

Foo1 foo11 foo111 foo2 foo22 foo222

What is my purpose? I want to replace the last three places of foo with 0. For example, if foo1 is changed to foo001, foo22 is changed to foo022, and foo111 and foo222 are not changed.

So what should we do? First, I use this command: rename foo foo0 foo

Is a wildcard, representing a character, that is, I will change the foo part of these files to foo0. Think about the changes.

[Root @ centos-01test] # rename foo foo0 foo

[Root @ centos-01test] # ls

Foo01 foo02 foo11 foo111 foo22 foo222

Do you see it? foo represents a file containing four characters and foo. We changed foo to foo0 using the command, so this effect will be achieved. Next we will use this command: rename foo foo0 foo

[Root @ centos-01test] # rename foo foo0 foo

[Root @ centos-01test] # ls

Foo001 foo002 foo011 foo022 foo111 foo222

As you can see, all five characters such as foo will be modified. As you can see, the rename command is used to master the structure, but to use wildcards.

Next, let's test several wildcard characters. * Represents any character

[Root @ centos-01test] # ls

Foo001 foo002 foo011 foo022 foo111 foo222

[Root @ centos-01test] # rename foo ofo *

[Root @ centos-01test] # ls

Ofo001 ofo002 ofo011 ofo022 ofo111 ofo222

[Root @ centos-01test] # rename ofo ccc ofo * 1

[Root @ centos-01test] # ls

Ccc001 ccc011 ccc111 ofo002 ofo022 ofo222

[Root @ centos-01test] # rename 1 c ccc *

[Root @ centos-01test] # ls

Ccc00c ccc0c1 cccc11 ofo002 ofo022 ofo222 // you noticed that it was changed from the first occurrence of 1.

For example, we can also change the end file name to be similar.

[Root @ centos-01bobo] # ls

1. htm 2.htm 3.htm 4.htm 5.mmm

[Root @ centos-01bobo] # rename. htm. html *. htm

[Root @ centos-01bobo] # ls

1. html 2.html 3.html 4.html 5.mmm

[Root @ centos-01bobo] # rename. html @ html *. html

[Root @ centos-01bobo] # ls

1 @ html 2 @ html 3 @ html 4 @ html 5.mmm

How about it? Is this command quite powerful. However, it is faster and more flexible to create and modify files in large batches using scripts.

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.