【Linux】批量重命名文件小技巧
发布日期:2021-09-30 14:06:48 浏览次数:10 分类:技术文章

本文共 1708 字,大约阅读时间需要 5 分钟。

操作系统:CentOS Linux release 7.4.1708 (Core)

需求:需要将.txt文件的后缀改为.log

需要命令:rename

[root@test test]# rename -hUsage: rename [options] expression replacement file...Options: -v, --verbose    explain what is being done -s, --symlink    act on symlink target -h, --help     display this help and exit -V, --version  output version information and exitFor more details see rename(1).

可以看到rename的使用方法是 rename [选项]  替换前的内容 替换后的内容 替换文件

试验样例:

[root@test test]# touch {1..10}.txt[root@test test]# lltotal 0-rw-r--r--. 1 root root 0 Feb 26 10:59 10.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 1.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 2.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 3.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 4.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 5.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 6.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 7.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 8.txt-rw-r--r--. 1 root root 0 Feb 26 10:59 9.txt[root@test test]# rename -v .txt .log *.txt`10.txt' -> `10.log'`1.txt' -> `1.log'`2.txt' -> `2.log'`3.txt' -> `3.log'`4.txt' -> `4.log'`5.txt' -> `5.log'`6.txt' -> `6.log'`7.txt' -> `7.log'`8.txt' -> `8.log'`9.txt' -> `9.log'[root@test test]# lltotal 0-rw-r--r--. 1 root root 0 Feb 26 10:59 10.log-rw-r--r--. 1 root root 0 Feb 26 10:59 1.log-rw-r--r--. 1 root root 0 Feb 26 10:59 2.log-rw-r--r--. 1 root root 0 Feb 26 10:59 3.log-rw-r--r--. 1 root root 0 Feb 26 10:59 4.log-rw-r--r--. 1 root root 0 Feb 26 10:59 5.log-rw-r--r--. 1 root root 0 Feb 26 10:59 6.log-rw-r--r--. 1 root root 0 Feb 26 10:59 7.log-rw-r--r--. 1 root root 0 Feb 26 10:59 8.log-rw-r--r--. 1 root root 0 Feb 26 10:59 9.log

ps:网上有很多类似这种rename 's/替换前内容/替换后内容/' 需要替换文件,rename并不能真正重命名。具体情况使用rename -h 查看帮助文档进行操作

转载地址:https://blog.csdn.net/imliuqun123/article/details/114119646 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Linux】awk文本替换
下一篇:【Linux】环境变量配置出错后,提示command not found

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年03月28日 13时55分27秒