Linux Command tip: How to rename multiple files in Linux

Source: Internet
Author: User
Tags perl script

Linux Command tip: How to rename multiple files in Linux

 

I know that I can use the mv command to rename the file. But what if I want to rename many files? It would be boring to do this for every file. Is there a way to rename multiple files at a time?

In Linux, you can use the mv command to change the file name. However, mv cannot rename multiple files with wildcards. You can use sed, awk, or xargs to process multiple files. However, these command lines are cumbersome and unfriendly, and are prone to errors if you are not careful. You do not want to cancel the error name of the 1000 files!

When you want to rename multiple files, the rename tool may be the simplest, safest, and most powerful command line tool. This rename Command is actually a Perl script that is pre-installed on all current Linux distributions.

Change File Extension

Assume that you have many. JPEG image files. You want to change their name to .jpg. The following command changes the. JPEG file to *. jpg.

$ Rename's/\. jpeg $/\. jpg/'*. jpeg

Change uppercase to lowercase, and vice versa.

Sometimes you want to change the case sensitivity of the file name, you can use the following command.

Change all files to lowercase letters:

# Rename 'y/A-Z/a-z /'*

Change all files to uppercase:

# Rename 'y/a-z/A-Z /'*

Change file name Mode

Now let's consider more complex regular expressions that contain subpatterns. In PCRE, the sub-mode is included in parentheses, followed by a number (for example, $1, $2) after the $ sign ).

For example, the following command changes 'imgnnnn.w.'dannn.jpg '.

# Rename-v's/img _ (\ d {4}) \. jpeg $/dan _ $1 \. jpg/'*. jpeg

Img_5417.jpeg renamed as dan_5417.jpg

Img_5418.jpeg renamed as dan_5418.jpg

Img_5419.jpeg renamed as dan_5419.jpg

Img_541_jpeg renamed as dan_541_jpg

Img_5421.jpeg renamed as dan_5421.jpg

For example, the following command will convert 'img_000nnnn.w.'into 'dan_nnnn.jpg '.

# Rename-v's/img _ \ d {3} (\ d {4}) \. jpeg $/dan _ $1 \. jpg/'* jpeg

Img_0005417.jpeg renamed as dan_5417.jpg

Img_0005418.jpeg renamed as dan_5418.jpg

Img_0005419.jpeg renamed as dan_5419.jpg

Img_000540000jpeg renamed as dan_540000jpg

Img_0005421.jpeg renamed as dan_5421.jpg

In the preceding example, the sub-mode '\ d {4}' captures four consecutive numbers. The four captured numbers are $1, which will be used for the new file name.

 

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.