@ Rename usage in Linux-Batch rename @

Source: Internet
Author: User
Tags perl regular expression zip extension

The Linux RENAME Command has two versions: one is the C language version and the other is the Perl language version. The earlier Linux releases basically use the C language version, now it is hard to see the C language version. due to historical reasons, when the Perl language is booming, Linux Tool developers believe that Perl can replace C, therefore, most of the tools in C have been rewritten by Perl. Because Perl supports regular expression processing, it is more powerful and no longer requires the C language version.

1. How do I differentiate the version of the RENAME command in the system?

Enter man Rename to see the first line is
Rename (1) Linux programmer's Manual Rename (1)
This is the C language version. [View the C language version on the system]
Rename (1) Perl programmers reference guide Rename (1)
This is the Perl version!

Syntax differences between the two versions:
C language, according to The Man annotation above,
The syntax format of rename is:
Rename fromtofile
This command has three parameters: From: Modify the name, to: Change the name, and file to be modified.
Usage example:
For example, there are a batch of files starting with log, log001.txt, log002.txt ...... until log100.txt
Now I want to replace all the logs of these files with history.
Rename log history log * [C usage ~]
The meaning of this command is clear. Replace the log characters in all files starting with log with history.
The replaced file is history001.txt and history002.txt until history100.txt.
In the rename C language, another man example isBatch modification of suffix names,
For example, we want to change all JPEG image file extensions to JPG files.
Rename .w..jpg *. JPEG
In this way, all extensions with. JPEG extensions are modified to. jpg.
To sum up:

Functions available in the C language version of rename:Modify file names in batchesThe result is that each file will be replaced with the same string! That is to say, we cannot implement loops and rename them by number!

2. Batch Rename of the Perl version. The benefit of Perl is that you can use regular expressions to complete very strange functions.

Perl parameter format:
Rename perlexprfiles
Note: In Perl, rename has only two parameters. The first parameter is a Perl regular expression, and the second parameter is the file to be processed.
Man rename help example:
1) There are a batch of files ending with. Bak. Now I want to remove all these. BAK files.
Rename's/\. Bak $ // '*. Bak
This command is very simple, because I have not studied Perl in the system. I don't know if it is the same as replacing strings in Perl, but sed does this, therefore, if you have sed or Tr basics, it is easy to understand that this replacement is exactly the same as the regular syntax in sed.
2) Change the name of all files containing uppercase/lowercase letters to lowercase letters.
Rename 'y/A-Z/a-z /'*
It is still the same as the SED replacement syntax. You do not need to explain it more. If you cannot understand it, You can systematically learn sed first.
There are several more practical examples:

1) Remove spaces in file names in batches
Linux File names do not support spaces and do not know when they are allowed. Of course, when calling a file through the command line, spaces are very problematic, for example, you can directly use mv oldfile newfile, but it won't work if there are spaces. You have to add double quotation marks: mv "oldfile" "newfile" or use a backslash to transfer \ []. It's okay, however, if you directly introduce the image name containing SpacesLatex documentWhen latex generates a PDF file, it will print the file name directly. This problem has been bothering me for a long time. How can I always see the file name in my generated PDF? Then I found out that the file name contains spaces! The file name generated in Windows system contains spaces. Although annoying, some pictures generated by the HP scanner are added with spaces by default. There is no way to remove them, before the system studies the RENAME command, I used
Remove spaces from music videos.
The following two versions of the online flow to remove spaces:

1) tr version:

find . -type f -name "* *" -print |while read name; dona=$(echo $name | tr ' ' '_')if [[ $name != $na ]]; thenmv "$name" $nafidone

I used this version all the time. I don't know which Internet I searched for. At that time, I didn't have the system to learn the TR/SED/awk command.
It is easy to understand. The "find. Type F-name" ** "-print" clause is used to find all types of common files in the current directory and their namesFiles with spacesAnd print it out. In fact, find prints
-Print is redundant, and is transmitted to the while loop through the pipeline. The file name is put in the name variable. Use the tr command to replace spaces with underscores. If the names after execution are different, use the MV command to rename the name. However, this if judgment is dispensable, because find has queried all file names containing spaces, then after the tr command, the $ na variable is definitely not equal
$ Name variable.
Therefore, this code can be simplified:

find . -type f -name "* *" |while read name; dona=$(echo $name | tr ' ' '_')mv "$name" "$na" done

Tr can be viewed as a simplified version of sed. tr uses underscores to replace spaces.
There is also the SED version implementation:
For f in *; Do MV "$ F" 'echo "$ F" | SED's/[] \ +/_/G''; done
The SED expression can also be written as follows:
Sed's/[[: Space:] \ +/_/G'
Remember, the plus sign that appears once or multiple times in sed must be added with a backslash. That is:\+.
Well, these two methods are too cool. Let's look at the rename implementation:
Rename's/[] +/_/G '*
OK is that simple.
The spaces in square brackets can be replaced by [: Space,
That is, it can be written as's/[[: Space:] +/_/G'
Note that the rename uses the standard Perl regular syntax, so you do not need to change the plus sign to the backslash plus sign.
That is, + cannot be changed to \ +. Otherwise, the replacement fails.

There are several interesting examples:
For example, add hello to the file header.
Rename's/^/Hello /'*
..Html extension changed to. htm
Rename's/. html $/. htm /'*
Append the. Zip extension at the end:
Rename's/$/. Zip /'*
Unified remove. Zip Suffix:

Rename's/. Zip $ //'*

Regular numeric number names, such as 1.jpg, 2.jpg... 100.jpg. Now, all three file names must be 1.jpg... 001.jpg.

Run the following two commands:

Rename's/^/00/'{0-9}.jpg # in this step, change 1.jpg... 9.jpg to 001.jpg... 009.jpg.

Rename's/^/0/'%0-9%0-9%.jpg # This step changes 10.jpg... 99.jpg to 010.jpg... 090.jpg

Okay, rename has studied so much that we do not know how to introduce dynamic variables in Rename for the moment, such as $ I ++

I tested I = 0; rename-n "s/^. * $/$ (+ + I)/"* after execution, I is incremented by 1, instead of adding one in each file as I imagined, this assumption may be due to the batch Implementation of rename, resulting in ++ I computing only once!

-N is used to test the rename process and does not run directly. You can view the test result and run it again.

Now, let's explain again. You must confirm the version of your language when using it. I am a C language version ~

Rename (1) Linux programmer's Manual Rename (1)


Rename from to file...


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.

Rename. htm. html *. htm
Will fix the extension of your HTML files.

Here is an example:

Finally, let's look at the following figure for a question in practical application ~

Now we can see that we want to replace the ad letter in the image file name with big [Note: copy a copy and cannot replace it directly]. Then we can think about how to do this. By the way, we use rename ~


Find./-name "* _ad.jpg"-exec CP "{}" {}. 1 \;

Find./-name "* _ad.jpg. 1"-exec renamead.jpg.1big.jpg {}\;

If it can be replaced directly, a command is provided:


Find./-name "* _ad.jpg"-exec rename
Ad big {}\;

See the following tests ~

In fact, scripts can also be used, that is, loop statements ~

#! /Bin/sh # mail: zhuying.jz.137@163.comfor file in 'ls. /* _ad.jpg 'do rename = 'echo "$ {file }. 1 "'/bin/CP $ File $ rename/bin/mv $ rename 'echo" $ {rename % Ad * invalid big.jpg "' # here is a new method for intercepting variables, this method is used in the shell programming course. Done

ActuallyQuestions about. * and *Sometimes I am confused, for example:

[oracle@sor-sys ~]$ echo ${PATH##.*:}/usr/local/ruby-1.8.7-p330/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.6.0_05/bin:/usr/local/maven/bin:/root/bin:/usr/local/jdk1.6.0_05/bin:/home/oracle/bin[oracle@sor-sys ~]$ echo ${PATH##*:}/home/oracle/bin

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.