(转)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可以 ...
随机推荐
- Poj1258_Agri-Net(最小生成树)
一.Description(poj1258) Farmer John has been elected mayor of his town! One of his campaign promises ...
- poj3414Pots(倒水BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13231 Accepted: 5553 Special J ...
- Dubbo注册中心的四种配置方式详解
Dubbo目前支持4种注册中心,(multicast,zookeeper,redis,simple) 推荐使用Zookeeper注册中心. 一.Multicast注册中心 不需要启动任何中心节点,只要 ...
- 使用python对文件夹里面所有代码行数进行统计。
统计目录下所有的代码个数和总行数. # -*- coding: utf-8 -*- # @Author : ydf import json import os from pathlib import ...
- linux命令-rpm查询包
安装了哪些rpm包呢 [root@wangshaojun Packages]# rpm -qa /////查看全部安装的包 [root@wangshaojun Packages]# rpm -qa l ...
- angularJS中自定义指令
学习了angularJS一周,但是大部分时间被自定义指令占用了.博主表示自学互联网好心塞的,发现问题的视觉很狭窄,这比解决问题要更难.这篇文章首先介绍了自定义,然后介绍了在使用自定义指令遇到的问题. ...
- mysql软文
常用的MySQL复杂查询语句写法 http://www.blogjava.net/bolo/archive/2015/02/02/422649.html mysql sql常用语句大全 http: ...
- XML解析,出现ClassCastException 原因
import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.Docum ...
- python fabric的安装与使用
背景:fabric主要执行远程的shell命令,包括文件的上传.下载,以及提示用户输入等辅助其它功能. 测试系统:ubuntu16 要求:python //系统有自带,ubuntu16 通常自带pyt ...
- shell批量创建用户随机密码
批量创建用户随机密码企业面试题3:批量创建10个系统帐号usr01-usr10并设置密码(密码为随机8位字符串). #! /bin/bash . /etc/init.d/functions Path= ...