Linux系统linux rename 命令,批量重命名文件一般是一项频繁的任务,rename命令提供了一个快速有效的方法来完成这项工作。这个强悍的命令可以通过模式匹配和正则表达式来更改文件名。

Linuxrename命令介绍

rename命令是用于批量重命名文件的工具。它可以按照用户指定的替换条件来修改文件名。这个条件一般是一种正则表达式,特别适宜对文件名进行复杂的更改。

Linuxrename命令适用的Linux版本

rename命令在许多Linux发行版中都是可用的,如Ubuntu,Fedora,Debian等。并且linux rename 命令,个别发行版,例如CentOS可能没有预安装这个命令。

[linux@bashcommandnotfound.cn ~]$ sudo yum install prename

[linux@bashcommandnotfound.cn ~]$ sudo dnf install prename

Linuxrename命令的基本句型

句型格式:

rename [options] 's/old/new/' files

Linuxrename命令的常用选项或参数说明选项说明

-v

显示详尽的重命名信息

-n

演示重命名操作但不实际执行

-f

覆盖现有文件

-h

显示帮助信息

-V

显示版本信息

Linuxrename命令实例解读实例1:简单的重命名操作

将所有.txt文件的扩充名改为.md:

[linux@bashcommandnotfound.cn ~]$ rename 's/.txt$/.md/' *.txt

实例2:演示模式下的重命名

查看重命名操作的疗效linux软件linux操作系统教程,而不实际执行:

[linux@bashcommandnotfound.cn ~]$ rename -n 's/.txt$/.md/' *.txt

实例3:覆盖文件时的重命名

假如目标文件名存在,使用-f选项强制覆盖:

[linux@bashcommandnotfound.cn ~]$ rename -f 's/.txt$/.md/' *.txt

实例4:将文件名中的空格替换为顿号

[linux@bashcommandnotfound.cn ~]$ rename 's/ /_/' *

实例5:将文件名转为大写

[linux@bashcommandnotfound.cn ~]$ rename 'y/A-Z/a-z/' *

实例6:添加前缀

给所有JPEG文件添加前缀”vacation_”:

[linux@bashcommandnotfound.cn ~]$ rename 's/^/vacation_/' *.jpeg

实例7:消除数字

删掉文件名中所有的数字:

[linux@bashcommandnotfound.cn ~]$ rename 's/[0-9]//' *

实例8:批量更改文件扩充名

将所有.jpg文件更名为.jpeg:

[linux@bashcommandnotfound.cn ~]$ rename 's/.jpg$/.jpeg/' *.jpg

实例9:使用正则表达式分组

将文件名”photo1.jpg”重命名为”1_photo.jpg”:

[linux@bashcommandnotfound.cn ~]$ rename 's/(photo)(d+)/$2_$1/' photo1.jpg

实例10:详尽模式的重命名

详尽输出每次重命名的信息:

[linux@bashcommandnotfound.cn ~]$ rename -v 's/.txt$/.md/' *.txt

实例11:递归重命名

递归地在子目录中重命名所有.txt文件为.md:

[linux@bashcommandnotfound.cn ~]$ find . -type f -name '*.txt' -exec rename 's/.txt$/.md/' {} +

实例12:安全地防止覆盖文件

在重命名时检测目标文件名是否已存在,以防止数据遗失:

[linux@bashcommandnotfound.cn ~]$ rename -n 's/.txt$/.md/' *.txt

实例13:将日期从文件名移至开头

Tagged:
Author

这篇优质的内容由TA贡献而来

刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

发表回复