【Linux】批量重命名文件小技巧

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

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

需要命令:rename

[root@test test]# rename -h

Usage:
 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 exit

For more details see rename(1).

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

试验样例:

[root@test test]# touch {1..10}.txt
[root@test test]# ll
total 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]# ll
total 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 查看帮助文档进行操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值