(转)rename命令详解
rename命令详解:
原文:http://www.cnblogs.com/amosli/p/3491649.html
对文件重命名是常用的操作之一,一般对单个文件的重命名用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(1) Perl Programmers Reference Guide RENAME(1) NAME
rename - renames multiple files
..................

很明显是Perl版的,如果输入man rename出现下方提示,那说明是C版:
RENAME(1) Linux Programmer’s Manual RENAME(1)
这里由于没怎么接触过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 2 amosli amosli 4096 12月 26 00:30 ./
drwxrwxr-x 5 amosli amosli 4096 12月 25 23:58 ../
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:26 a.txt
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:30 Untitled Document
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:30 Untitled Document 2 amosli@amosli-pc:~/learn/example$ rename -v 's/ /_/g' *
Untitled Document renamed as Untitled_Document
Untitled Document 2 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 8
drwxrwxr-x 2 amosli amosli 4096 12月 26 00:35 ./
drwxrwxr-x 5 amosli amosli 4096 12月 25 23:58 ../
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:26 a
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:30 Untitled_Document
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:30 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 120
drwxrwxr-x 5 amosli amosli 4096 12月 26 00:40 ./
drwxr-xr-x 69 amosli amosli 4096 12月 25 22:11 ../
---------- 1 amosli amosli 3 12月 18 22:49 a1
-rw-rw-r-- 1 amosli amosli 3 12月 18 22:49 a2
-rw-rw-r-- 1 amosli amosli 3 12月 18 22:49 a3
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:39 a.mp3
-rw-rw-r-- 1 amosli amosli 16 12月 23 01:49 args.txt
-rw-rw-r-- 1 amosli amosli 0 12月 26 00:39 b.mp3
-rw-rw-r-- 1 amosli amosli 11 12月 23 01:45 cecho.sh
-rw-rw-r-- 1 amosli amosli 79 12月 19 00:56 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命令每个命令都可以拿出来仔细研究,但想深入研究,一定要掌握正则,正则表达式,主流语言都支持,对文本处理有很大帮助,接下来的篇幅将会研究一些正则表达式。
(转)rename命令详解的更多相关文章
- rename命令详解
基础命令学习目录首页 原文链接:http://man.linuxde.net/rename 将main1.c重命名为main.c rename main1.c main.c main1.c renam ...
- linux shell 脚本攻略学习9--rename命令详解
rename命令详解: 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosli@amosli-pc:~/learn/example$ ls abc.txt amosli@a ...
- Docker命令详解
Docker命令详解 最近学习Docker,将docker所有命令实验了一番,特整理如下: # docker --help Usage: docker [OPTIONS] COMMAND [arg ...
- DOS命令详解
DOS命令详解 命令 \? 可以进入命令帮助 1.md命令创建目录. MKDIR [drive:]pathMD [drive:]path 如果命令扩展被启用,MKDIR 会如下改变: 如果需要,MKD ...
- db2常用命令(详解)大全
近一年来在项目开发中使用到了IBM的DB2 9.1的数据库产品,跟Oracle相比一些命令有很大的区别,而它最大的功能是支持 xml存储.检索机制,通过XPath进行解析操作,使开发人员免于 ...
- net user命令详解
net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...
- [转帖]dd命令详解
dd命令详解 https://czmmiao.iteye.com/blog/1748748 之前一直对linux的命令很恐惧 现在发现 其实不是那么复杂 要仔细学习就可以了 比如 dd = disk ...
- 【文本处理命令】之sed命令详解
sed行处理命令详解 一.简介 sed命令是一种在线编辑器.一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作.sed是按行来处理文本内容的,它一次处理一行内容.处理时,把当前处 ...
- 抓包工具:tcpdump抓包命令详解
抓包工具:tcpdump抓包命令详解 简介: tcpdump全称:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以 ...
随机推荐
- JVM插庄之一:JVM字节码增强技术介绍及入门示例
字节码增强技术:AOP技术其实就是字节码增强技术,JVM提供的动态代理追根究底也是字节码增强技术. 目的:在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于对应用程序的二进制文件进行修 ...
- 基于ftp服务实现yum网络共享
安装ftp服务:yum install vsftpd 安装后: CentOS7 启动服务:systemctl start vsftpd 设置开机启动:systemctl enable vsftpd 同 ...
- C语言学习笔记--struct 和 union关键字
1.struct关键字 C 语言中的 struct 可以看作变量的集合struct中的每个数据成员都有独立的存储空间. 结构体与柔性数组 (1)柔性数组即数组大小待定的数组 (2)C 语言中可以由结构 ...
- CentOS 7 安装以及配置 VNC
一.安装VNC : yum install tigervnc tigervnc-server -y 二.启动vnc并设置密码: vncserver :1 三.将服务添加到防火墙: firewall ...
- 第 2 章 Python 语言入⻔
目录 2.1低而长的学习曲线 2.2Python的优势 2.3在你的计算机中安装Python 2.4如何运行Python程序 2.5文本编辑器 2.6寻求帮助 Python语言是一种流行的编程语言,在 ...
- kafka之c接口常用API------librdkafka
1 安装方法以及相关库文件 https://github.com/edenhill/librdkafka 2 High-level producer High-level consumer Simpl ...
- byte和int转换
byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); b3=b1+b ...
- Windows使用Github
首先,你需要执行下面两条命令,作为 git 的基础配置,作用是告诉 git 你是谁,你输入的信息将出现在你创建的提交中. git config --global user.name "你的名 ...
- 高考是最后一次拼智商的事了。(beacuse 大多数人的努力程度之低根本轮不到拼天赋!)
高考是最后一次拼智商的事. —因为大多数人的努力程度之低 根本轮不到拼天赋 在这个不起眼的小公司实习也有两周了,周四经理说说为了增加IOS开发小组和安卓开发小组之间的交流,准备每周开一次这种报告会. ...
- IPMITOOL 配置BMC用户设置
IPMITOOL 配置BMC用户设置 本文档共介绍5条ipmi设置user的命令,这些命令需要使用root权限才能使用,其中- H为需要操作的BMC ip,-I lanplus为使用rmcp+协议发送 ...