linux批量重命名后缀名,Linux 批量重命名文件的方法

SYNOPSIS

rename from to file...

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

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

file 表示目标文件。

[[email protected] 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

[[email protected] tmp]# rename ".log" ".jpg" *

[[email protected] 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:

[[email protected] 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处理:

[[email protected] tmp]# ls|sed -nr "s#(^.*[0-9].)(.*)#mv & \1log#gp"|bash

[[email protected] 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再配合替换字符串

[[email protected] 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:

[[email protected] tmp]# ./rename.sh |bash

[[email protected] 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值