rename命令详解:

文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如:

amosli@amosli-pc:~/learn/example$ ls
abc.txt
amosli@amosli-pc:~/learn/example$ mv abc.txt a.txt
amosli@amosli-pc:~/learn/example$ ls
a.txt

那么如何对指文件进行重命名呢?当然你可以全部手动去赋值,但很影响效率,如下,将所有文件名称都改为大写的,如何做呢?

amosli@amosli-pc:~/learn/example$ ls
a.txt b.txt c.txt d.txt e.txt
amosli@amosli-pc:~/learn/example$ rename 'y/a-z/A-Z/' *
amosli@amosli-pc:~/learn/example$ ls
A.TXT B.TXT C.TXT D.TXT E.TXT

如果用mv命令可能手动要花很长时间,但rename命令一句就搞定了。

下面来介绍rename命令工具:

Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,早期的Linux发行版基本上使用的是C语言版本的,现在已经很难见到C语言版本的了,因为Perl版本的支持正则处理,所以功能更加强大,基本上现在linux下默认的rename命令都是Perl版的。

如何查看系统里的rename命令是哪个版本的?

输入man rename命令,我的是ubuntu12.04,出现下面的提示:

amosli@amosli-pc:~/learn/example$ man rename

RENAME()                                        Perl Programmers Reference Guide                                        RENAME()

NAME
rename - renames multiple files
..................

很明显是Perl版的,如果输入man rename出现下方提示,那说明是C版:

RENAME() Linux Programmer’s Manual RENAME()

这里由于没怎么接触过C版的,就不做介绍了。

Perl语言是公认的正则表达式之王,对正则的支持相当给力,所以在linux命令里都能使用正则。

rename的语法中就有正则:

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

在linux的rename help提示下有如下一段话:

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.

大致意思是rename命令修改符合后面条件的文件的文件名,只有符合perlexpr的文件名才会被修改,否则将不会被修改。

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.

对于参数:

-v 表示会显示修改成功的文件名;

-n 则表示不执行任何操作,主要用来测试rename过程,并不直接运行,可以查看测试效果后,然后再运行;

-f 则表示会强制修改。

例:

drwxrwxr-x  amosli amosli  12月  : ./
drwxrwxr-x amosli amosli 12月 : ../
-rw-rw-r-- amosli amosli 12月 : a.txt
-rw-rw-r-- amosli amosli 12月 : Untitled Document
-rw-rw-r-- amosli amosli 12月 : Untitled Document amosli@amosli-pc:~/learn/example$ rename -v 's/ /_/g' *
Untitled Document renamed as Untitled_Document
Untitled Document renamed as Untitled_Document_2

这里用到的是-v参数,'s/ /_/g' 正则表示的是将空格替换为_, *则表示应用于所有文件。

amosli@amosli-pc:~/learn/example$ rename     's/\.txt$//'       *.txt

则表示删除所有txt 的文件后缀名,执行结果如下:

amosli@amosli-pc:~/learn/example$ ll
total
drwxrwxr-x amosli amosli 12月 : ./
drwxrwxr-x amosli amosli 12月 : ../
-rw-rw-r-- amosli amosli 12月 : a
-rw-rw-r-- amosli amosli 12月 : Untitled_Document
-rw-rw-r-- amosli amosli 12月 : Untitled_Document_2

将所有目标.mp3文件移入指定的目录中,则可以用:

find . -type f -name '*mp3' -exec mv {} ../ \; 

例:

amosli@amosli-pc:~/learn/example$ find . -type f -name '*mp3';
./b.mp3
./a.mp3
amosli@amosli-pc:~/learn/example$ find . -type f -name '*mp3' -exec mv {} ../ \;
amosli@amosli-pc:~/learn/example$ ls
amosli@amosli-pc:~/learn/example$ cd ..
amosli@amosli-pc:~/learn$ ll
total
drwxrwxr-x amosli amosli 12月 : ./
drwxr-xr-x amosli amosli 12月 : ../
---------- amosli amosli 12月 : a1
-rw-rw-r-- amosli amosli 12月 : a2
-rw-rw-r-- amosli amosli 12月 : a3
-rw-rw-r-- amosli amosli 12月 : a.mp3
-rw-rw-r-- amosli amosli 12月 : args.txt
-rw-rw-r-- amosli amosli 12月 : b.mp3
-rw-rw-r-- amosli amosli 12月 : cecho.sh
-rw-rw-r-- amosli amosli 12月 : debug.sh

将文件名称的大写全部转换为小写则为:

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

将*.JPG更名为*.jpg则可以用:

rename *.JPG *.jpg

其他参数:rename支持的参数相当多,-a 到-z均有,如需获取更多信息可以输入如下命令:

man rename
h

会列出一系列的参数列表,如下:

。。。。。。。。。
-? ........ --help
Display help (from command line).
-a ........ --search-skip-screen
Forward search skips current screen.
-A ........ --SEARCH-SKIP-SCREEN
Forward search always skips target line.
-b [N] .... --buffers=[N]
Number of buffers.
-B ........ --auto-buffers
Don't automatically allocate buffers for pipes.
-c ........ --clear-screen
Repaint by clearing rather than scrolling.
-d ........ --dumb
Dumb terminal.
-D [xn.n] . --color=xn.n
Set screen colors. (MS-DOS only)
-e -E .... --quit-at-eof --QUIT-AT-EOF
Quit at end of file.
-f ........ --force
Force open non-regular files.
-F ........ --quit-if-one-screen
Quit if entire file fits on first screen.
-g ........ --hilite-search
Highlight only last match for searches.
-G ........ --HILITE-SEARCH
Don't highlight any matches for searches.
-h [N] .... --max-back-scroll=[N]
Backward scroll limit.
-i ........ --ignore-case
Ignore case in searches that do not contain uppercase.
。。。。。。

可以根据自己需要进行使用。

linux命令每个命令都可以拿出来仔细研究,但想深入研究,一定要掌握正则,正则表达式,主流语言都支持,对文本处理有很大帮助,接下来的篇幅将会研究一些正则表达式。

linux shell 脚本攻略学习9--rename命令详解的更多相关文章

  1. linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解

    文件权限详解 一.chmod命令详解 文件权限和所有权是Unix/Linux文件系统最显著的特征之一.linux中的每一个文件都与多种权限类型相关联,在这些权限中主要分类为3种: 用户(User)是文 ...

  2. linux shell 脚本攻略学习6-xargs详解

    xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. 例如,下面的命令: rm `find /path -type f` ...

  3. linux shell 脚本攻略学习20--awk命令入门详解

    awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...

  4. linux shell 脚本攻略学习16--wc命令详解,tree命令详解

    在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...

  5. linux shell 脚本攻略学习14--head命令详解,tail命令详解

    当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...

  6. linux shell 脚本攻略学习13--file命令详解,diff命令详解

    一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...

  7. linux shell 脚本攻略学习11--mkdir和touch命令详解

    一.创建目录(mkdir命令详解) amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amo ...

  8. linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解

    一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...

  9. linux shell 脚本攻略学习7---tr命令详解

    tr命令详解 什么是tr命令?tr,translate的简写,translate的翻译: [trænsˈleit] vi. 翻译, 能被译出 vt. 翻译, 解释, 转化, 转变为, 调动 在这里用到 ...

随机推荐

  1. java 使用AXIS调用远程的web service

    1.服务 2.代码 import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.ax ...

  2. js的技巧

    字典: if(tac["detail"][sid] || tac["detail"][sid]==0) //判断某项是否存在,0为真 tac["det ...

  3. Docker-machine创建虚机时停在虚机启动的提示上,并且创建的虚机显示Ip Not found

    Docker-machine创建虚机时停在虚机启动的提示上,并且创建的虚机用docker-machine ls 列出来的时候显示Ip Not found, 是什么原因那? [答案] 看这个帖子: ht ...

  4. 【Nodejs】外研社一年级起三年级下MP3下载爬虫1.00

    //====================================================== // 外研社一年级起三年级下MP3下载爬虫1.00 // 2018年5月9日 //== ...

  5. phpstudy 开启apache反向代理

    vhosts.conf 1 2 3 4 5 6 7 8 9 10 11 <VirtualHost *:8080> ServerAdmin webmaster@dummy-host.exam ...

  6. RPi Desktop盒子安装与服务配置

    批量安装配置盒子时候,可以先安装一个,其余的从这台copy过去. 之前的部分shell记录在本地,记录如下,以免忘记.下次可直接cp执行即可: Step1, 创建用户/组 sudo groupadd ...

  7. 【Linux】好玩的Linux命令(二)

    关于Linux talk:http://man.linuxde.net/talk 下面文章转自:http://www.oschina.net/translate/11-lesser-known-use ...

  8. 利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  9. jconsole监控远程linux tomcat运行情况的配置 (转)

    来自:http://zhumeng8337797.blog.163.com/blog/static/100768914201242494649455/ 步骤如下: 1.编辑tomcat/bin/cat ...

  10. iOS开发,更改状态栏(StatusBar)文字颜色为白色

    详细实现步骤 1.如图在Info.plist中进行设置,主要用于处理启动画面中状态栏(StatusBar)文字颜色. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...