奇怪的rename

sh/bash/dash/ksh/zsh等Shell脚本
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

奇怪的rename

#1

帖子 pityonline » 2009-11-15 0:28

奇怪的rename

今天又学习了一下rename,是一个配合正则表达式来重命名的一个强大工具。照我的习惯先看一下帮助文件。
这是man rename的显示:
rename(3tcl) Tcl Built-In Commands rename(3tcl)

_________________________________________________________________________________________________

NAME
rename - 重命名或删除一个命令

总览 SYNOPSIS
rename oldName newName
_________________________________________________________________

描述 DESCRIPTION
把 过 去叫做 oldName 的命令重命名为叫做 newName。如果 newName 是一个空串则删除叫做 oldName
的命令。oldName 和 newName 可以包括名字空间限定符(包含名字空间的名字)。如果一个命令被重 命
名 到一个不同名字空间中,将来对它的调用将在新的名字空间中执行。rename 命令返回一个空串作为
结果。

参见 SEE ALSO
namespace(n), proc(n)

关键字 KEYWORDS
command, delete, namespace, rename

[中文版维护人]
寒蝉退士

[中文版最新更新]
2001/10/28

《中国 Linux 论坛 man 手册页翻译计划》:
http://cmpp.linuxforum.net

Tcl rename(3tcl)
rename没有--help选项,如果rename --help查看后会提示:

代码: 全选

Unknown option: ?
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
因为帮助太少,所以只能自己摸索。现照网上一些说明测试:

新建一个test目录,下面放入a,b,c,d,e五个文件。

代码: 全选

mkdir test
cd test
touch a b c d e 
ls查看一下显示为

代码: 全选

a  b  c  d  e
将每个文件都加上.txt后缀

代码: 全选

rename 's/$/.txt/' *
ls查看一下显示为

代码: 全选

a.txt  b.txt  c.txt  d.txt  e.txt
删除则将.txt替换为空

代码: 全选

rename 's/.txt//' *
ls显示为

代码: 全选

a  b  c  d  e
在每个文件都加上test-前缀

代码: 全选

rename 's/^/test-/' *
ls显示为

代码: 全选

test-a  test-b  test-c  test-d  test-e
删除前缀则将test-替换为空

代码: 全选

rename 's/test-//' *
ls显示每个文件的test-前缀都被删除了。

现将每个文件都加后缀.txt,然后将每个文件名都由小写改为大写

代码: 全选

rename 's/$/.txt/' *; rename 'y/a-z/A-Z/' *
ls显示为

代码: 全选

A.TXT  B.TXT  C.TXT  D.TXT  E.TXT
发现连后缀也被改为大写了,看着很别扭,还是改回来吧。rename命令有-v选项,意为执行的同时显示执行过程:

代码: 全选

rename 'y/A-Z/a-z/' * -v
这是过程显示:

代码: 全选

A.TXT renamed as a.txt
B.TXT renamed as b.txt
C.TXT renamed as c.txt
D.TXT renamed as d.txt
E.TXT renamed as e.txt
ls查看一下

代码: 全选

a.txt  b.txt  c.txt  d.txt  e.txt
rename的-n选项则为仅测试,并不实际更改文件名
rename 's/.txt//' * -n
这是测试显示,与执行过程显示是一样的:

代码: 全选

a.txt renamed as a
b.txt renamed as b
c.txt renamed as c
d.txt renamed as d
e.txt renamed as e
ls查看一下,实际文件名并没有改变

代码: 全选

a.txt  b.txt  c.txt  d.txt  e.txt
选项中的-v和-n是很实用的,如果不确定重命名是否成功可先用-n测试一下,另外还有一个选项是-f,应该是强制执行的意思,此时不显示执行过程,与不加选项差不多。

单引号后面第一个字母为什么是s?是start还是select?不知道,于是用rename '字母/^/0/' * -n测试了一下其它字母,发现从a到z,m,q,s是有定义的,其中s正常显示执行过程,m与q显示为:

代码: 全选

Number found where operator expected at (eval 1) line 1, near "m/^/0"
syntax error at (eval 1) line 1, near "m/^/0"
其它字母仅显示一句

代码: 全选

syntax error at (eval 1) line 1, near "/^"
但事实上y是有定义的,用来转换大小写。在前面的测试中没有特殊提示,估计是格式不同。于是用rename '字母/a-z/A-Z/' * -n测试其它字母,发现m,q,s,y是有定义的,其中y正常显示执行过程,s无任何显示,m与q显示为:

代码: 全选

Bareword found where operator expected at (eval 1) line 1, near "m/a-z/A"
syntax error at (eval 1) line 1, near "m/a-z/A"
其它字母均显示:

代码: 全选

Warning: Use of "-z" without parentheses is ambiguous at (eval 1) line 1.
syntax error at (eval 1) line 1, near "a-z"
在该位置上又用rename '其它字符/^/0/' * -n其它字符测试了一下,显示如下:

代码: 全选

rename '*/^/0/' * -n
Argument "*main::/" isn't numeric in bitwise xor (^) at (eval 1) line 1.
Argument "*main::/" isn't numeric in bitwise xor (^) at (eval 2) line 1.
Argument "*main::/" isn't numeric in bitwise xor (^) at (eval 3) line 1.
Argument "*main::/" isn't numeric in bitwise xor (^) at (eval 4) line 1.
Argument "*main::/" isn't numeric in bitwise xor (^) at (eval 5) line 1.

代码: 全选

rename '^/^/0/' * -n
syntax error at (eval 1) line 1, near "^"

代码: 全选

rename '$/^/0/' * -n
Argument "\n" isn't numeric in bitwise xor (^) at (eval 1) line 1.
Argument "\n" isn't numeric in bitwise xor (^) at (eval 2) line 1.
Argument "\n" isn't numeric in bitwise xor (^) at (eval 3) line 1.
Argument "\n" isn't numeric in bitwise xor (^) at (eval 4) line 1.
Argument "\n" isn't numeric in bitwise xor (^) at (eval 5) line 1.

代码: 全选

rename '-/^/0/' * -n
Unknown option: /
Unknown option: ^
Unknown option: /
Unknown option: 0
Unknown option: /
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
因为正则没学过,暂时没有找到规律性重命名文件名中的不规则部分的办法,也没有找到将文件名中所有前缀规律性重命名为00-99的办法,所以这次测试意义不大,以后有机会继续学习……
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 奇怪的rename

#2

帖子 tusooa » 2009-11-15 14:37

你的rename和我的不一样啊

代码: 全选

15/11/2009 14:33:57
tusooa (~)
>> rename
call: rename from to files...
15/11/2009 14:34:00
tusooa (~)
>> rename --version
rename (util-linux-ng 2.16)

代码: 全选

] ls -ld //
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

Re: 奇怪的rename

#3

帖子 pityonline » 2009-11-15 14:46

tusooa 写了:你的rename和我的不一样啊

代码: 全选

15/11/2009 14:33:57
tusooa (~)
>> rename
call: rename from to files...
15/11/2009 14:34:00
tusooa (~)
>> rename --version
rename (util-linux-ng 2.16)
狂晕!相当不一样,我的rename连--version选项都没有……

代码: 全选

rename --version
Unknown option: version
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
我的manpage是用的中文的,里面连版本号都没提,并且显示
[中文版最新更新]
2001/10/28
这个rename有年头了啊,比ubuntu历史还早……
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

Re: 奇怪的rename

#4

帖子 pityonline » 2009-11-15 15:11

终于看到正经的rename的manpage了,那个中文的有点儿不正经……

代码: 全选

File: *manpages*,  Node: rename,  Up: (dir)

RENAME(1)              Perl Programmers Reference Guide              RENAME(1)

NAME
       rename - renames multiple files

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

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified
       as the first argument.  The perlexpr argument is a Perl expression
       which is expected to modify the $_ string in Perl for at least some of
       the filenames specified.  If a given filename is not modified by the
       expression, it will not be renamed.  If no filenames are given on the
       command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the
       extension, you might say

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

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

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.

ENVIRONMENT
       No environment variables are used.

AUTHOR
       Larry Wall

SEE ALSO
       mv(1), perl(1)

DIAGNOSTICS
       If you give an invalid Perl expression you'll get a syntax error.

BUGS
       The original "rename" did not check for the existence of target
       filenames, so had to be used with care.  I hope I've fixed that (Robin
       Barker).

perl v5.10.0                      2009-06-26                         RENAME(1)
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 奇怪的rename

#5

帖子 HuntXu » 2009-11-15 15:28

楼主仔细看看你一楼那个man是tcl里的rename...
HUNT Unfortunately No Talent...
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

Re: 奇怪的rename

#6

帖子 pityonline » 2009-11-15 15:56

HuntXu 写了:楼主仔细看看你一楼那个man是tcl里的rename...
rename与tcl有何关系,为什么man rename会出现tcl里的rename呢?大家的rename都是哪里装的,软件包的全名是什么?
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
onelynx
帖子: 817
注册时间: 2008-11-13 16:03

Re: 奇怪的rename

#7

帖子 onelynx » 2009-11-15 17:01

好像各个发行版的rename会有差异,这是我fedora下的 man rename
RENAME(1) Linux Programmer’s Manual RENAME(1)

NAME
rename - Rename files

SYNOPSIS
rename from to file...
rename -V

DESCRIPTION
rename will rename the specified files by replacing the first occurrence of from in their name
by to.

-V, --version
Display version information and exit.

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.

And
rename .htm .html *.htm

will fix the extension of your html files.

SEE ALSO
mmv(1), mv(1)

AVAILABILITY
The rename command is part of the util-linux-ng package and is available from ftp://ftp.ker-
nel.org/pub/linux/utils/util-linux-ng/.
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 奇怪的rename

#8

帖子 aerofox » 2009-11-15 20:10

有两个不同版本的 rename,具体是哪个,看自己机器中的 man。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 奇怪的rename

#9

帖子 eexpress » 2009-11-15 20:15

gprename 有预览结果的
● 鸣学
头像
zhu527812567
帖子: 883
注册时间: 2009-11-17 12:29
联系:

Re: 奇怪的rename

#10

帖子 zhu527812567 » 2009-11-17 22:24

看MAN PAGE前看下所属章节。。
rename(3tcl) Tcl Built-In Commands rename(3tcl)

3表明是library functions..

后缀TCL表示这是TCL的一个库函数- -

虽然它这里号称Built-in Commands。。

一般程序的话 man 1 foo
管理程序man 8 foo
其他章节都不是程序。。

另LS那个rename也有,不过被重命名为rename.ng了

而还有个PerlProgramming带的rename那是一个perl脚本
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

Re: 奇怪的rename

#11

帖子 pityonline » 2009-11-17 23:54

zhu527812567 写了:看MAN PAGE前看下所属章节。。
rename(3tcl) Tcl Built-In Commands rename(3tcl)

3表明是library functions..

后缀TCL表示这是TCL的一个库函数- -

虽然它这里号称Built-in Commands。。

一般程序的话 man 1 foo
管理程序man 8 foo
其他章节都不是程序。。

另LS那个rename也有,不过被重命名为rename.ng了

而还有个PerlProgramming带的rename那是一个perl脚本
原来如此,学习了!

楼上刚注册半天就发了35个贴子,厉害!
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 奇怪的rename

#12

帖子 tusooa » 2009-11-27 21:39

zhu527812567 写了:看MAN PAGE前看下所属章节。。
rename(3tcl) Tcl Built-In Commands rename(3tcl)

3表明是library functions..

后缀TCL表示这是TCL的一个库函数- -

虽然它这里号称Built-in Commands。。

一般程序的话 man 1 foo
管理程序man 8 foo
其他章节都不是程序。。

另LS那个rename也有,不过被重命名为rename.ng了

而还有个PerlProgramming带的rename那是一个perl脚本
ubuntu里面是rename.ul
不知道那个prename哪来的,perl的sh Configure???

代码: 全选

] ls -ld //
头像
cyl19880415
帖子: 1
注册时间: 2011-09-08 15:18

Re: 奇怪的rename

#13

帖子 cyl19880415 » 2011-09-20 18:04

学习了。
上次由 cyl19880415 在 2011-09-21 13:09,总共编辑 1 次。
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 奇怪的rename

#14

帖子 lilydjwg » 2011-09-20 22:55

不就是两个 rename 嘛,自己 man 下或者 file 下就知道是哪个了。一个是 perl 写的,一百多行,另一个是 C 写的。ArchLinux 下 rename 是 C 的那个,perl 的叫 perl-rename。
头像
tenzu
论坛版主
帖子: 36924
注册时间: 2008-11-21 20:26

Re: 奇怪的rename

#15

帖子 tenzu » 2011-09-20 23:00

P哥的技术帖
回复