Getting started with Linux: Rename multiple files in Linux

Source: Internet
Author: User

Getting started with Linux: Rename multiple files in Linux

Q: I know that I can use the mv command to rename a 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.

The basic syntax of the RENAME Command is as follows.

  1. Rename [-v-n-f] <pcre> <files>

<Pcre> is a Perl-Compatible Regular Expression that indicates the file to be renamed and how to do it. The regular expression format is's/old-name/new-name /'.

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

The '-n' option tells the rename command to display the file rename without actually changing the name. This option is useful when you want to simulate changing the file name without changing the file name.

The '-F' option forcibly overwrites existing files.

Next, let's take a look at several real examples of the rename Command.

Change File Extension

Assume that you have many. JPEG image files. You want to change their names. Jpg. The following command willChange the. jpeg file to *. jpg.

  1. $ 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:

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

Change all files to uppercase:

  1. # 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 willChange NNNN.jpeg to 'DanNNNN.jpg '.

  1. # Rename-v's/img _ (\ d {4}) \. jpeg $/dan _ $1 \. jpg/'*. jpeg
  1. Img_5417.jpeg renamed as dan_5417.jpg
  2. Img_5418.jpeg renamed as dan_5418.jpg
  3. Img_5419.jpeg renamed as dan_5419.jpg
  4. Img_541_jpeg renamed as dan_541_jpg
  5. Img_5421.jpeg renamed as dan_5421.jpg

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

  1. # Rename-v's/img _ \ d {3} (\ d {4}) \. jpeg $/dan _ $1 \. jpg/'* jpeg
  1. Img_0005417.jpeg renamed as dan_5417.jpg
  2. Img_0005418.jpeg renamed as dan_5418.jpg
  3. Img_0005419.jpeg renamed as dan_5419.jpg
  4. Img_000540000jpeg renamed as dan_540000jpg
  5. 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.

This article permanently updates the link address:

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.