How to batch rename files in Linux

Source: Internet
Author: User
Tags lowercase regular expression perl script

In Linux, when you want to change a filename, use the MV command. However, MV cannot use a wildcard rename characters to name multiple files. You can use SED, awk, or with Xargs to work with multiple files. However, these command lines are cumbersome and unfriendly, and error prone 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.

The code is as follows:

Rename [-v-n-F]

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

The '-V ' option displays details of the filename 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 if you want to simulate changing the file name without changing the filename.

The '-F ' option forces the overwriting of existing files.

Next, let's look at a few 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 changes the. jpeg file to *.jpg.

The code is as follows:

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

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:

The code is as follows:

# rename ' y/a-z/a-z/' *

Change all files to uppercase:

The code is as follows:

# rename ' y/a-z/a-z/' *

Change file name mode

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

For example, the following command will turn ' imgnnnn.jpeg ' into ' dannnnn.jpg '.

The code is as follows:

# 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_5420.jpeg renamed as Dan_5420.jpg

Img_5421.jpeg renamed as Dan_5421.jpg

For example, the following command will turn ' img_000nnnn.jpeg ' into ' dan_nnnn.jpg '.

The code is as follows:

# 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_0005420.jpeg renamed as Dan_5420.jpg

Img_0005421.jpeg renamed as Dan_5421.jpg

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

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.