zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux批量文件重命名命令_shell重命名文件指令

Linux文件批量命令shell 指令 重命名
2023-06-13 09:13:41 时间

大家好,又见面了,我是你们的朋友全栈君。

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/197409.html原文链接:https://javaforall.cn