How to rename multiple files under Linux

Source: Internet
Author: User
Tags perl script

In Linux, when you want to change a file name, use the MV command. However, MV cannot use wildcard rename characters to name multiple files. You can use SED, awk, or in combination with Xargs to handle multiple files. However, these command lines are cumbersome and unfriendly, and are prone to error if you are not careful. You don't want to undo the wrong name of 1000 files!

When you want to rename multiple files, the Rename tool is probably the simplest, safest, and most powerful command-line tool. This rename command is actually a Perl script that is preinstalled on all Linux distributions now.

The following is the basic syntax for renaming commands.

is a Perl-compatible regular expression that represents the file to be renamed and what to do. The regular expression is in the form of ' s/old-name/new-name/'.

The '-V ' option displays the details of the file name change (for example: XXX renamed to YYY).

The '-n ' option tells the rename command to show that the file will be renamed without actually changing the name. This option is useful in situations where you want to simulate changing the file name without changing the file name.

The '-f ' option enforces overwriting of existing files.

Below, let's look at a few practical examples of the rename command.

Change the file name extension

Suppose you have a lot of. jpeg picture files. You want to change their names to. jpg. The following command will change the. jpeg file to *.jpg.

uppercase to lowercase, and vice versa

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

Change all files to lowercase:

Change all files to uppercase:

Change the file name pattern

Now let's consider more complex regular expressions that contain sub-patterns. In Pcre, the sub-pattern is enclosed in parentheses, and the $ character is followed by a number (such as $1,$2).

For example, the following command will change ' imgnnnn.jpeg ' to ' dannnnn.jpg '.

 

For example, the following command will change ' img_000nnnn.jpeg ' to ' dan_nnnn.jpg '.

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

The source of this article: Linux China Community

How to rename multiple files under Linux

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.