Batch file name modification under Linux

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

The rename of Perl Classic code

Larry Wall's rename script, which uses only a single-digit line of code, constructs a super-powerful batch file renaming tool:

Comments:

1. # touch rename.perl // Create a file:rename.perl, the contents are as follows.

2. # chmod +x Rename.perl

3. # CP rename.perl/bin/

4.  # rename.perl  ' s/gongxu/leibie/g '   *    // gongxu is replaced with leibie .

#!/usr/bin/perl-w
# rename-larry ' s filename fixer
#用法: perl expression [filename to process]
$ = or die "Usage:rename expr [files]\n"

#如果没有给出要处理的文件名则从标准输入读入
chomp(@ARGV = <STDIN>) unless @ARGV;

for (@ARGV) {
$ was = $_;
Eval $op; #对待处理的文件名 ($_) performs a Perl expression for user input $op
die $@ if $@; #退出, if Eval is wrong
Rename($ was,$_) unless $ was eq $_;
}

A classic example of the rename script demonstrates:

% Rename . Perl ' s/\.orig$//' *.orig #移除文件末尾的. Orig

% Rename . Perl "Tr/a-z/a-z/unless/^make/" * #所有非Make打头的文件名大写转为小写

% Rename . Perl ' $_. = '. Bad ' ' *.f #每个 *.f file appended with a. bad

% Rename . Perl ' print ' $_: "; s/foo/bar/if =~/^y/i ' * #回显每个待处理的文件名, wait for input, if user input starts with Y or y, replace Foo in the file name with bar

% find/tmp-name "*~"-print | rename . Perl ' s/^ (. +) ~$/.#$1/' #把/tmp directory each file name at the end of the file name is changed to. #开头




+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Http://www.linuxidc.com/Linux/2011-01/31594.htm

There are two versions of the Linux Rename command, one in the C language 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 the Perl version supports regular processing, so it's more powerful. The C language version is no longer required.

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 from to File

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!


2, the Perl version of batch renaming, with Perl The advantage is that you can use regular expressions to complete the very peculiar function.

The format of the parameters for the Perl version:

Rename perlexpr files

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 is no space, you have to add double quotes: MV "oldfile" "NE Wfile "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$//' *

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.


++++++++++++++++++++++++++++++++++++++++++++++++++++++


http://apps.hi.baidu.com/share/detail/16475516

1.rename Command Batch modified file name, in fact, Linux can use other methods to batch change the file name, but rename is too convenient

For example, change all the tables to cdb1_* to cdb_*.
In this directory
Only need # rename ' cdb1 ' CDB ' *

Used to write a for loop to do ... Think about how silly ah, hehe

Rename also has more features that suggest man rename under

From:http://www.hao32.com/unix-linux/42.html

2. Batch change file name rename

With the man rename command you can tell that the rename command is actually a Perl script command,
It is dedicated to renaming multiple files in batches (rename multiple files).

command format:
Rename [-v] [-n] [-f] perlexpr [files]
Perlexpr is a regular expression in Perl script format.
Parameters:
-V,--verbose
Verbose:print Names of files successfully renamed.
Verbose mode: Print List of file names for successful changes
-N,--no-act
No action:show What files would has been renamed.
Test mode: Do not really execute commands, but just show which filenames should be done
Change, for test mode.
-F,--force
Force:overwrite existing files.
Mandatory mode: Overwrite already exists when changing file name if changed files already exist
The file.

Typical applications of rename:

0. Batch Change file name extensions
$ ls
1.txt 2.txt 3.txt 4.txt
$ Rename ' s/\.txt/\.ext/' *
$ ls
1.ext 2.ext 3.ext 4.ext
1. Delete file name extensions in bulk
$ ls
1.txt 2.txt 3.txt 4.txt
$ Rename ' s/\.txt//' *
$ ls
1 2 3 4
2. Batch Add File extension
$ ls
1 2 3 4
$ Rename ' s/$/\.txt/' *
$ ls
1.txt 2.txt 3.txt 4.txt
3. Batch rename files on your own terms
$ ls
1.ext 2.ext 3.ext 4.ext
$ Rename ' s/(\d)/chapter $/' *
$ ls
1th Chapter Ext 2nd Chapter EXT 3rd Chapter EXT 4th Chapter Ext

3.

Method 1: Split the file name processing, you can arbitrarily modify the filename find-name ' *.log '-printf%f\\n|awk-f '. ' ' {print $} ' |xargs-i{} mv {}.log Xiyun_{}.log Method 2: Use rename command under Rename General Linux to compare simple rename ' Test ' Xiyun ' *.log ' Replacing a part of a string in a file name with the Rename command in Ubuntu supports regular expressions and is therefore more powerful. Method 3: Use the Find and Xargs and MV directly, without awk in the middle, so you can add only the suffix name, cannot modify the file name. For your safety, please only open URLs with reliable sources

Open Site Cancellation

From: http://hi.baidu.com/vchentech/blog/item/7d40ed2376e82f589922ed6b.html
Read (14299) | Comments (1) | Forwards (2) |2

Previous: Learn the biggest benefits of Linux

Next: Embedded (linux+arm) circuit diagram

Related Popular articles
    • AIX PVID Vgid Modify
    • Python QQ Bulk Login
    • The sequel to my first Python program ...
    • Foundation of AWK
    • Ubuntu Next idea Breakpoint removal ...
    • Common Linux Service ports
    • Xmanager 2.0 for Linux configuration
    • "Rootfs build" BusyBox httpd ...
    • OpenWrt in Luci study notes
    • What is a shell?
    • Linux DHCP Peizhi ROC
    • Soft links to Unix files
    • What does this command mean, I'm new ...
    • What does sed-e "/grep/d" mean ...
    • Who can help me solve Linux 2.6 10 ...
Leave something to the owner! ~~

Appleyuchi2013-05-19 06:36:51

Brother:
Hello!
I am the semiconductor industry, Linux under the circuit design ~
Can you please write more popular Ah, can't understand ...
For example, I want to change the end of all files under/home/appleyuchi. T.z for. tar.gz
How to write the command ah, I do not understand what you write the order of S is what it means ah, thank you!

Reply | Report Comment on the hot topic

Batch file name modification under Linux

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.