rename
rename
用字符串替换的方式批量改变文件名
补充说明
rename命令存在两个版本用法上有所区别
C语言版本, 支持通配符
[常用通配符说明]
? 表示一个任意字符
* 表示一个或一串任意字符
Perl版本, 支持正则表达式
[常用正则表达式符号说明]
^ 匹配输入的开始位置
$ 匹配输入的结尾
. 匹配除换行符外的任意字符
+ 匹配前一个字符一次或多次 例如,"zo+"可以匹配"zoo",但不匹配"z"
[a-z] 表示某个范围内的字符,例如,"[a-z]"匹配"a"与"z"之间的任何一个小写字母字符。
[^m-z] 否定的字符区间。与不在指定区间内的字符匹配。
区分方法: rename --version
如果返回结果中包含 util-linux , 说明是C语言版本, 反之是Perl版本
# Perl版本 | Ubuntu(18),Mint(20)默认的是Perl版本
$ rename --version
/usr/bin/rename using File::Rename version 1.10
# C语言版本 | Centos(7)默认的是C语言版本
$ rename --version
rename,来自 util-linux 2.23.2
语法
# Perl版本
rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -e|-E perlexpr]*|perlexpr [ files ]
# C语言版本
rename [选项] 表达式 替换的字符 文件...
参数
# Perl版本
-v, --verbose 详细:成功重命名的文件的打印名称。
-0, --null 从STDIN读取时,请使用\0作为记录分隔符
-n, --nono 不执行任何操作:打印要重命名的文件名,但不重命名。
-f, --force 覆盖:允许覆盖现有文件
--path, --fullpath 重命名完整路径:包括任何目录组件。默认
-d, --filename, --nopath, --nofullpath 不重命名目录:仅重命名路径的文件名部分
-h, --help 帮助:打印提要和选项。
-m, --man 手册: 打印手册页.
-V, --version 版本: 显示版本号.
-e 表达: 作用于文件名的代码.
可以重复来构建代码(比如“perl-e”)。如果没有-e,则第一个参数用作代码。
-E 语句:对文件名执行操作的代码,如-e,但终止于 ';'.
# C语言版本
-v, --verbose 提供视觉反馈,其中重命名了哪些文件(如果有的话)
-V, --version 显示版本信息并退出。
-s, --symlink 在符号链接目标上执行重命名
-h, --help 显示帮助文本并退出
Perl版本
将1.txt 2.txt重命名为1.log 2.log
$ rename -v "s/txt/log/g" 1.txt 2.txt
1.txt renamed as 1.log
2.txt renamed as 2.log
修改文件的后缀
rename "s//.html//.php/" * # 把.html 后缀的改成 .php后缀
批量添加文件后缀
rename "s/$//.txt/" * # 把所有的文件名都以txt结尾
批量删除文件名
rename "s//.txt//" * # 把所有以.txt结尾的文件名的.txt删掉
C语言版本
将1.txt 2.txt重命名为1.log 2.log
$ rename -v txt log 1.txt 2.txt
`1.txt' -> `1.log'
`2.txt' -> `2.log'
案例
[root@localhost130 yechangyao]# ls
Centos-7.repo cout_test.txt Documents epel-7.repo hello.sh news.txt Public test001.tar test1108.txt test3 test.txt ttxt.txt yechangyao
core.10299 Desktop Downloads expr.sh Music Pictures Templates test002.tar.gz test2 testFor.sh text1108.txt Videos
[root@localhost130 yechangyao]# rename --version
rename from util-linux 2.23.2
[root@localhost130 yechangyao]# rename txt log ttxt.txt
[root@localhost130 yechangyao]# ls
Centos-7.repo cout_test.txt Documents epel-7.repo hello.sh news.txt Public test001.tar test1108.txt test3 test.txt tlog.txt yechangyao
core.10299 Desktop Downloads expr.sh Music Pictures Templates test002.tar.gz test2 testFor.sh text1108.txt Videos
[root@localhost130 yechangyao]# rename log txt tlog.txt
[root@localhost130 yechangyao]# ls
Centos-7.repo cout_test.txt Documents epel-7.repo hello.sh news.txt Public test001.tar test1108.txt test3 test.txt ttxt.txt yechangyao
core.10299 Desktop Downloads expr.sh Music Pictures Templates test002.tar.gz test2 testFor.sh text1108.txt Videos
[root@localhost130 yechangyao]# rename .txt .log ttxt.txt
[root@localhost130 yechangyao]# ls
Centos-7.repo cout_test.txt Documents epel-7.repo hello.sh news.txt Public test001.tar test1108.txt test3 test.txt ttxt.log yechangyao
core.10299 Desktop Downloads expr.sh Music Pictures Templates test002.tar.gz test2 testFor.sh text1108.txt Videos
[root@localhost130 yechangyao]#
但愿日子清静抬头遇见的都是柔情
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?