Linux under Rename Usage--Batch Rename

Source: Internet
Author: User
Tags perl regular expression

There are two versions of the Linux Rename command, one in C and one in Perl, and the earlier Linux distributions are basically in the C language, and it's hard to see the C language version, because of historical reasons, in the Perl language rounds, Linux tool developers believe that Perl can replace C, so most of the tools are originally C version of Perl rewrite, because the Perl version of the support of the regular processing, so the function is more powerful, it is no longer required in the C language version.

1. How can I tell which version of the Rename command is in the system?

Enter man rename See the first line is
RENAME (1) Linux Programmer ' s Manual RENAME (1)
So this is the C language version. "I'm looking at the C-language version on the system."
And if it appears that:
RENAME (1) Perl Programmers Reference Guide RENAME (1)
This is the Perl version!

Syntax differences for two versions:
C language, according to man above the annotations,
The syntax format for rename is:
Rename Fromtofile
The command has three parameters, from: What name to change, to: What name to change, and what file it needs to modify.
Usage examples:
For example, there are a number of files, all start with log, Log001.txt, Log002.txt .... Until Log100.txt.
Now you want to replace the log of this batch of files with the history
Rename log History log* "C's Usage ~"
The meaning of this command is clear, replace the log character in all files beginning with log with the history
This replaced file is: History001.txt, history002.txt ..... Until History100.txt.
Another example of the Rename C language version is the batch modification of the suffix name,
For example, we want to change all JPEG suffix image files to jpg files.
Rename. Jpeg.jpg*.jpeg
In this way, all suffix names with the. jpeg extension are all modified to. jpg
Now summarize:

Rename C language version of the ability to achieve: Batch modification of file names, the result is that each file will be replaced with the same string! That is, you cannot implement such a loop and then rename it by number!

2. A Perl version of batch renaming, with the benefit of Perl, is that you can use regular expressions to accomplish very peculiar functions.

The format of the parameters for the Perl version:
Rename Perlexprfiles
Note that the Perl version of rename has only two parameters, the first parameter is a Perl regular expression, the second parameter is the file to be processed
Example of a man rename help:
1) There is a batch of files that end with. Bak and now want to remove all these. bak.
Rename ' s/\.bak$//' *.bak
This command is simple because I haven't learned Perl yet, and I don't know if Perl replaces the string in this way, but SED does so, so if you have sed or TR basics, it's easy to see that this substitution is identical to the regular syntax in SED.
2) Change all filenames to lowercase letters with size letters.
Rename ' y/a-z/a-z/' *
is still the same as the SED replacement grammar, do not have to explain, if you do not understand, you can systematically learn the SED first.
There are a few more practical examples:

1) Remove the space in the file name in bulk
The Linux file name originally does not support spaces, do not know when to allow, of course, in the command line when the file is called, the space is very problematic, such as you can be directly MV Oldfile NewFile but there are spaces on the die, you have to add double quotes: MV "Oldfile" "New File "or use a backslash to transfer \[], this is OK, but if you directly put the picture name containing the space into the latex document, Latex generated PDF will print out the name of the file, the problem has plagued me for a long time, I generated the PDF always appear file name? Later found that the file name contains a blank space problem! Windows system generated by the file name is born with a space, although it is annoying, but some of the images generated by the HP scanner added a blank space, there is no way, had to remove him, before the System Research Rename command, I use the MV to remove the space.
Two de-whitespace versions of the online process:

1) TR version:
Find. -type f-name "* *"-print |
while read name; Do
na=$ (echo $name | tr "_")
if [[$name! = $na]]; Then
MV "$name" $na
Fi
Done
This version of the previous I have been using, do not know which online ransacked, there was no systematic study of the Tr/sed/awk command.
Note, well understood, find. Type F-name "* *"-print this sentence is to find the current directory of all types of ordinary files and the name of the file containing spaces, and print out, in fact, find the default is to print this-print redundant, and then through the pipeline to the while loop read, the file name is placed in In the name variable, replace the space with the TR command as an underscore. The following determines if the name after execution is different, use the MV command to rename. But this if judgment is optional, because find has queried all the filenames contain spaces, then after the TR command, $NA variable is certainly not equal to the $name variable.
So this code can be simplified:
Find. -type f-name "* *" |
while read name; Do
na=$ (echo $name | tr "_")
MV "$name" "$na"
Done
TR can be seen as a lite version of SED, and TR uses underscores to replace spaces.
There is also an SED version implementation:
For f in *;d o mv "$f" ' Echo ' $f | Sed ' s/[]\+/_/g '; Done
The SED expression here can also be written like this:
Sed ' s/[[:space:]]\+/_/g '
Remember, however, that one or more occurrences of the plus sign in SED need to be added with backslashes. That is: \+, so you can.
Well, these two ways are too damn wordy, see rename realize it:
Rename ' s/[]+/_/g ' *
OK, that's so easy.
Spaces within square brackets can be replaced with [: space:],
That can be written as ' s/[[:space:]]+/_/g '
Note here that rename uses the standard Perl regular syntax, so there is no need to convert the plus sign to a backslash plus
That is, the + cannot be modified to \+, otherwise the substitution fails.

There are a couple of interesting examples:
For example, unity in the file header add Hello
Rename ' s/^/hello/' *
Unify the. html extension to. htm
Rename ' s/.html$/.htm/' *
Unify append. zip suffix at tail:
Rename ' s/$/.zip/' *
Unify remove. zip suffix:

Rename ' s/.zip$//' *

Regular numeric number names, such as 1.jpg, 2.jpg ..... 100.jpg, now to make the file name all three bits is 1.jpg .... 001.jpg

Run the Command two times:

Rename ' s/^/00/' [0-9].jpg # This step puts 1.jpg ... 9.jpg change to 001.jpg .... 009.jpg

Rename ' s/^/0/' [0-9][0-9].jpg # This step puts 10.jpg ... 99.jpg change to 010.jpg ..... 090.jpg

Ok, rename has studied so much, for the time being don't know how to introduce dynamic variables in rename, such as $i++

I have tested i=0; Rename-n "s/^.*$/$ ((++i))/" * After executing I was increased by 1, not as I imagined, can be in each operation of a file self-increment, conjecture may be due to rename batch implementation, resulting in ++i only calculated once!



-N is used to test the rename process, does not run directly, you can view the test results, and then run.
Well, once again, you must confirm your language version when you use it, my C language version ~

RENAME (1) Linux Programmer ' s Manual RENAME (1)

Function:

Rename from to File ...

Usage:

For example, given the files foo1, ..., foo9, Foo10, ..., foo278, the commands
Rename foo foo0 foo?
Rename foo foo0 foo??
Would turn them into foo001, ..., foo009, foo010, ..., foo278.

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

Let's look at an example:

Finally to a practical application of the problem, first look at the following figure ~

See, we want to change the image file name of the ad letter to Big "Note: Copy one, can not be replaced directly", then think about how to do it, right, is to use rename~

cd/data/openshop_1028/img_server/sources/goods/

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

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

If it were to be replaced directly, it would be an order:

cd/data/openshop_1028/img_server/sources/goods/

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

You can see the following tests ~

Reference http://www.2cto.com/os/201201/117383.html

Linux under Rename Usage--Batch Rename

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.