Summary of usage of Linux Rename command file renaming

Source: Internet
Author: User
Tags perl regular expression

The rename command in the Linux command is mainly used to rename the file, similar to the MV command, but rename can rename the batch file, while the MV command can only rename a single file, the following gives you a detailed description of the use of the Linux Rename command.

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.

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.

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*

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 what rename C can do: Batch modify file names, and 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!

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:

Remove spaces in file names in batches

The Linux file name originally does not support the space, do not know when to allow, of course, the command line when the file is called, the space is very problematic, such as you can be directly MV Oldfile NewFile but there is no space, you have to add double quotes: MV "Oldfile" " NewFile "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 before this vexed 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, file name 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. namely: \+

That's all you can do.

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.

The above is the use of Linux under the Rename command, the Rename command has two versions, most of the current use of the Perl version, you use this command should first check which version you use, in order to avoid errors.


This article from "Nicol Lock Silk Sorrow" blog, please make sure to keep this source http://smoke520.blog.51cto.com/9256117/1827786

Summary of usage of Linux Rename command file renaming

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.