rename (使用正则表达式重命名多个文件)

瑞兹 发表于 2021-02-19 09:10
浏览次数:
在手机上阅读

在类似Unix的操作系统上,named命令使用正则表达式重命名多个文件。它由Perl编程语言的创建者Larry Wall编写。

查看英文版

目录

1 rename 运行系统环境

2 rename 描述

3 rename 语法

4 rename 例子

rename 运行系统环境

Unix&Linux

rename 描述

rename命名根据正则表达式perlexpr重命名已命名的文件

如果给定文件未被表达式修改,则不会重命名。如果在命令行上未提供文件名,则将通过标准输入读取文件名。

rename renames the named files according to the regular expression perlexpr.

If a given file is not modified by the expression, it will not be renamed. If no file names are given on the command line, file names will be read via standard input.

查看英文版

查看中文版

rename 语法

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

选项

-v,-- verbose 详细:成功重命名的文件的打印名称。
-n--no-ACT 无操作:显示要重命名的文件。
-f--force 强制:覆盖现有文件。

Perl表达式:快速概述

所述perlexpr 参数是13759 Perl的编程语言中的正则表达式。Perl正则表达式是一个复杂而细微的主题,但是这里是一个简短的概述:

代换

用一个表达式替换另一个表达式,perlexpr的形式是:

s / expr1 / expr2 / [gi]

...其中,表达式1是描述一个表达式字符串要替换,和表达式2是描述你要替换的字符串的表达式。例如,

s/silly/foolish/

...将用字符串' foolish '代替字符串' silly '的首次出现。

要执行全局替换(即,将expr2替换为expr1的次数与expr1发生的次数一样多),请在替换表达式的末尾添加修饰符g。例如:

s/silly/foolish/g

...无论发生多少次,都将用“silly”代替每次“foolish”的发生。

要以不区分大小写的方式执行匹配,请在替换表达式的末尾添加一个i。例如,

s/silly/foolish/i

...将替代“SILLY”,“Silly”,或“siLLY”与“foolish”。

gi改性剂可以在两个相同的表达式来指定,以进行不区分大小写的全局替换,例如:

s/silly/foolish/gi

元字符

甲元字符是一个字符,其具有特殊的含义(或字符)。可以在表达式中使用它们来精确定义应匹配和替换的字符串。

这些是可以在Perl表达式中使用的一些常见的元字符:

元字符 意义
^ 匹配字符串的开头。
$ 匹配字符串的结尾。
. 匹配任何字符,除了换行符。
* 匹配零次或多次匹配前面字符或字符组的出现。
+ 一次或多次匹配前一个字符或一组字符的出现。
匹配前一个字符或一组字符出现零次或一次。

如果重复修饰符,后使用'?'指定应使用最短的匹配项。例如,' a {2,4}?即使“ aaa ”和“ aaaa ”也匹配,“ ”也会匹配“ aa ” 。请参阅下面的重复修饰符。
| 交替;行为类似于布尔“OR”。例如,“butter|jelly”将与buuterjelly匹配。
 ...  分组。例如,“ (eg | le)gs ”将匹配“eggs”或“legs”。
[ ... ] 一组字符。例如,“ [abc] ”将匹配“ a ”或“ b ”或“ c ”。字符集可以定义为:

[字元] 匹配列出的任何字符
] 匹配xy之间(包括xy之间)的所有字符。例如,“ [ce] ”将匹配cde,而“ [az] ”将匹配任何小写字母。
[^个字符] 字符不匹配; 换句话说,匹配列出的字符以外的任何字符。也可以取反字符范围;例如,' [^ ad] '匹配除abcd之外的任何字符。
[\-] 匹配连字符(“ - ”)。
] 可以将多个字符范围连续放置在一个字符集中。例如,' [a-zA-Z] '匹配任何字母,大写或小写。
m [ , [ n ]] } 一个重复修饰符,至少匹配m字符,最多匹配n个字符。例如, '一个{2} '将匹配' AA ', '一个{2,4} '将匹配' AA ', ' AAA '或' AAAA '和' B {2,} '将匹配两个或更多连续的b个字符。
\ 转义一个元字符,以便按字面意义对待它。例如,“ \ + ”匹配字符“ + ”(而不是具有特殊的含义元字符加号)。
\ t 匹配制表符。
\ n 匹配换行符。
\ r 匹配回车符。
\ w 匹配归类为“单词”字符的任何单个字符(字母数字字符或下划线' _ ')。
\ W 匹配任何单个非“单词”字符。
\ s 匹配任何单个空格字符(空格,制表符,换行符)。
\ S 匹配任何单个非空白字符。
\ d 匹配任何数字字符。此开关等效于字符集' [0-9] '
\ D 匹配任何非数字字符。
\ b 与任何“单词边界”匹配的“零宽度”匹配断言。
\ B 与任何非“单词边界”匹配的“零宽度”匹配断言。

翻译

翻译类似于替代。它可以用于将一个字符串转换为另一个字符串。翻译表达式的指定如下:

y / charset1 / charset2 /

...按顺序将字符集charset1中的每个字符转换为字符集charset2中的相应字符。(这些集与上面的字符集一样,只不过您不需要将它们放在方括号中。)例如,翻译表达式:

y / abc / def /

...将每个字母a转换为字母d,每个b转换为e,等等。

这也适用于定义为范围的charset。例如:

y / az / AZ /

将每个小写字母转换为等效的大写字母。

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

Options

-v--verbose Verbose: print names of files successfully renamed.
-n--no-act No Action: show what files would have been renamed.
-f--force Force: overwrite existing files.

Perl Expressions: A Quick Overview

The perlexpr argument is a regular expression as used by the Perl programming language. Perl regular expressions is a complex and nuanced subject, but here is a brief overview:

Substitution

To substitute one expression for another, the form of perlexpr is:

s/expr1/expr2/[gi]

...where expr1 is an expression describing the string you want to replace, and expr2 is an expression describing the string you want to replace. For instance,

s/silly/foolish/

...would substitute the first occurrence of the string 'silly' with the string 'foolish'.

To perform global substitution (that is, to substitute expr2 for expr1 as many times as expr1 occurs), add the modifier g at the end of the substitution expression. For instance:

s/silly/foolish/g

...would substitute every occurrence of 'silly' with 'foolish', no matter how many times it occurs.

To perform matching in a case-insensitive manner, add an i at the end of the substitution expression. For instance,

s/silly/foolish/i

...would substitute 'SILLY', 'Silly', or 'siLLY' with 'foolish'.

The g and i modifiers may both be specified in the same expression, to perform case-insensitive global substitution, for example:

s/silly/foolish/gi

Metacharacters

metacharacter is a character (or characters) which has a special meaning. They can be used in an expression to precisely define which strings should be matched and replaced.

These are some common metacharacters that can be used in a Perl Expression:

metacharacters meaning
^ Matches the beginning of a string.
$ Matches the end of a string.
. Matches any character, except a newline.
* Matches occurrences of the preceding character, or group of characters, zero or more times.
+ Matches occurrences of the preceding character, or group of characters, one or more times.
? Match occurrences of the preceding character, or group of characters, zero or one times.

If used after a repetition modifier, '?' specifies that the shortest possible match should be used. For instance, 'a{2,4}?' will match 'aa' even if 'aaa' and 'aaaa' would also match. See repetition modifiers, below.
| Alternation; behaves like a boolean 'OR'. For instance, 'butter|jelly' will match either butter or jelly.
(...) Grouping. For instance, '(eg|le)gs' will match either 'eggs' or 'legs'.
[...] A set of characters. For instance, '[abc]' will match either 'a' or 'b' or 'c'. Character sets can be defined as:

[characters] Matches any one of the characters listed.
[x-y] Matches any in a range of characters between x and y, inclusive. For instance, '[c-e]' will match either cd, or e, and '[a-z]' will match any lowercase letter.
[^characters] Does not match characters; in other words, matches any character except those listed. Can also negate a character range; for instance, '[^a-d]' matches any character except abc, or d.
[\-] Matches the hyphen character ("-").
[x-yX-Z] Multiple character ranges can be placed in a character set consecutively. For instance, '[a-zA-Z]' matches any letter, uppercase or lowercase.
{m[,[n]]} A repetition modifier which matches at least m and at most n of the preceding characters. For instance, 'a{2}' will match 'aa', 'a{2,4}' will match either 'aa', 'aaa', or 'aaaa', and 'b{2,}' will match two or more consecutive b characters.
\ Escapes a metacharacter so that it is treated literally. For instance, '\+' matches a literal '+' (instead of the plus symbol having its special metacharacter meaning).
\t Matches a tab character.
\n Matches a newline character.
\r Matches a carriage return character.
\w Matches any single character classified as a "word" character (either an alphanumeric character or an underscore '_').
\W Matches any single non-"word" character.
\s Matches any single whitespace character (space, tab, newline).
\S Matches any single non-whitespace character.
\d Matches any digit character. This switch is equivalent to the character set '[0-9]'
\D Matches any non-digit character.
\b A "zero-width" matching assertion which matches any "word boundary".
\B A "zero-width" matching assertion which matches any non-"word boundary".

Translation

Translation is similar to substitution. It can be used to translate one string to another, character-for-character. Translation expressions are specified as follows:

y/charset1/charset2/

...where each character in the set charset1, in order, is to be translated into the corresponding character from the character set charset2. (These sets are just like the character sets above, except you don't need to put them in brackets.) For example, the translation expression:

y/abc/def/

...would translate every letter a into the letter d, every b into an e, etc.

This also works for charsets defined as ranges. For example:

y/a-z/A-Z/

Would translate every lowercase letter into its uppercase equivalent.

查看英文版

查看中文版

rename 例子

rename 's/\.jpeg$/.jpg/' *

重命名任何扩展名为“ .jpeg ”的文件,使其扩展名为“ .jpg”。

find -type f -name '*.jpg' | rename 's/holiday/honeymoon/'

对于所有扩展名为“ .jpg ”的文件,如果它们包含字符串“ holiday ”,则将其替换为“honeymoon”。例如,此命令会将文件“ ourholiday001.jpg ”重命名为“ ourhoneymoon001.jpg ”。

此示例还说明了如何使用find命令发送扩展名为.jpg-name'* .jpg')的文件列表(-type f),以通过管道(|)重命名。重命名然后从标准输入中读取其文件列表。

rename 's/\.bak$//' *.bak

重命名所有匹配“ * .bak ”的文件,以去除其扩展名的文件名。例如,此命令会将文件“ project.bak ”重命名为“ project ”。

rename 'y/A-Z/a-z/' *

重命名文件,以便将所有大写字母更改为它们的小写字母。

rename 's/\.jpeg$/.jpg/' *

Rename any files with the extension ".jpeg" to have the extension ".jpg."

find -type f -name '*.jpg' | rename 's/holiday/honeymoon/'

For all files with the extension ".jpg", if they contain the string "holiday", replace it with "honeymoon". For instance, this command would rename the file "ourholiday001.jpg" to "ourhoneymoon001.jpg".

This example also illustrates how to use the find command to send a list of files (-type f) with the extension .jpg (-name '*.jpg') to rename via a pipe (|). rename then reads its file list from standard input.

rename 's/\.bak$//' *.bak

Rename all files matching "*.bak" to strip the file name of its extension. For instance, this command would rename the file "project.bak" to "project".

rename 'y/A-Z/a-z/' *

Rename files such that all uppercase letters are changed to their lowercase equivalents.

查看英文版

查看中文版

其他命令行

renice | replace | rm | rmdir | rn | route | rpcinfo | rcp | readlink | rehash | rsh | rlogin | rmmod |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条