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

红太狼 2022-10-20 05:28 319阅读 0赞

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

发表评论

表情:
评论列表 (有 0 条评论,319人围观)

还没有评论,来说两句吧...

相关阅读

    相关 批量命名大量文件

    参考自《linux shell 脚本攻略(第2版)》 将一个文件夹下所有后缀为JPEG的文件重命名为后缀为jpg的文件,文件名不变。 当文件夹下的文件数量太多时,使用普通的

    相关 Python - 批量文件命名

      两个目标两个: 1. 输入一组文件名,进行批量重命名; 2. 输入一组目录名,批量重命名各个目录下的文件。 附加功能: 1. 可根据文件的创建日期对文件重新排序;

    相关 JAVA批量命名

    起因 原文件名太过于冗长,而且看起来肥肠的不爽,于是就想把它改掉!!! ![70][] 改完之后,现在这样就爽多了!!! ![70 1][] 代码