Linux 批量重命名文件的方法

1.使用rename命令

SYNOPSIS
rename from to file...

from 表示需要替换或者处理的字符,比如文件的扩展名,文件名.

to 表示对from处理之后的结果。

file 表示目标文件。

[root@liujianfan tmp]# ls
hello_10_2016-03-17.log  hello_4_2016-03-17.log  hello_8_2016-03-17.log
hello_1_2016-03-17.log   hello_5_2016-03-17.log  hello_9_2016-03-17.log
hello_2_2016-03-17.log   hello_6_2016-03-17.log
hello_3_2016-03-17.log   hello_7_2016-03-17.log

使用rename将.log改为.jpg

[root@liujianfan tmp]# rename ".log" ".jpg" *
[root@liujianfan tmp]# ls
hello_10_2016-03-17.jpg  hello_4_2016-03-17.jpg  hello_8_2016-03-17.jpg
hello_1_2016-03-17.jpg   hello_5_2016-03-17.jpg  hello_9_2016-03-17.jpg
hello_2_2016-03-17.jpg   hello_6_2016-03-17.jpg
hello_3_2016-03-17.jpg   hello_7_2016-03-17.jpg

2.使用sed:

[root@liujianfan tmp]# ls|sed -nr "s#(^.*[0-9].)(.*)#mv & \1log#gp"
mv hello_10_2016-03-17.jpg hello_10_2016-03-17.log
mv hello_1_2016-03-17.jpg hello_1_2016-03-17.log
mv hello_2_2016-03-17.jpg hello_2_2016-03-17.log
mv hello_3_2016-03-17.jpg hello_3_2016-03-17.log
mv hello_4_2016-03-17.jpg hello_4_2016-03-17.log
mv hello_5_2016-03-17.jpg hello_5_2016-03-17.log
mv hello_6_2016-03-17.jpg hello_6_2016-03-17.log
mv hello_7_2016-03-17.jpg hello_7_2016-03-17.log
mv hello_8_2016-03-17.jpg hello_8_2016-03-17.log
mv hello_9_2016-03-17.jpg hello_9_2016-03-17.log

最后通过管道给bash处理:

[root@liujianfan tmp]# ls|sed -nr "s#(^.*[0-9].)(.*)#mv & \1log#gp"|bash
[root@liujianfan tmp]# ls
hello_10_2016-03-17.log  hello_4_2016-03-17.log  hello_8_2016-03-17.log
hello_1_2016-03-17.log   hello_5_2016-03-17.log  hello_9_2016-03-17.log
hello_2_2016-03-17.log   hello_6_2016-03-17.log
hello_3_2016-03-17.log   hello_7_2016-03-17.log

3.使用for再配合替换字符串

[root@liujianfan tmp]# vim rename.sh 
#!/bin/bash
#This script is use to rename files
for name in `ls *.log`;
do
echo "mv $name ${name/.log/.txt}"
done

最后通过bash:

[root@liujianfan tmp]# ./rename.sh |bash
[root@liujianfan tmp]# ls
hello_10_2016-03-17.txt  hello_4_2016-03-17.txt  hello_8_2016-03-17.txt
hello_1_2016-03-17.txt   hello_5_2016-03-17.txt  hello_9_2016-03-17.txt
hello_2_2016-03-17.txt   hello_6_2016-03-17.txt  rename.sh
hello_3_2016-03-17.txt   hello_7_2016-03-17.txt

转载于:https://www.cnblogs.com/hzdwwzz/p/9903874.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值