转:关于rename命令ubuntu下的用法

时间:2021-08-04 02:41:40

 
下面是我的遭遇:
上午想批量改几个文件的名字,觉得mv在批量方面不够方便,百度到了rename这个命令,原谅我吧,我总是在百度不到结果时才去看google,以后还是少去百度的好
国内很多贴子都在说linux下没有rename这个命令,扯淡,不过是平常改名字用mv罢了,rename经常用在批量改名方面。
当然了, 也有不少贴子在说rename用法,可是我怎么看都是一模一样的文章被不断转载的。我没用过debian系之外的*nix的rename,但是起码 debian跟ubuntu的rename跟他们说的用法不一样(今晚ssh连接unix-center总是time out,等明天试试那里的freebsd跟fedora还有soloris的rename的用法去)
下面是网上的一般用法,声明一下,我没说这是错的,我只是说这个在debian-based系统里不能用,但是看到某个ubuntu用户的百度空间这样写我十分不爽:

rename命令是带3个参数而不是很多人认为的2个参数
…………
下面以例子加以说明:
如文件夹中有这些文件foo1, ..., foo9, foo10, ..., foo278,如果使用
rename foo foo0 foo?
则它只会把foo1到foo9的文件重命名为foo01到foo09,因为?通配符只能替代单个字符,所以被重命名的文件只是有4个字符长度名称的文件,文件名中的foo被替换为foo0。
…………

开始我不知道上面的内容在ubuntu下错的,照做了,结果不成功,显示

  • Bareword "foo" not allowed while "strict subs" in use at (eval 1) line 1.


仔细检查之后还是不行,经过google之后发现:
debian-based的系统下,rename没有上面那种用法,而是下面的:

On Debian-based distros it takes a perl expression and a list of files. you need to would need to use:
rename 's/foo/foox/' *

试验之,成功
貌似这个用法在不同的发行版之间通用.

 man rename

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

To translate uppercase names to lower, you'd use

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

更多:http://blog.csdn.net/disikexing/article/details/4154205