有时候我们有一个目录下面的文件名都需要重新命令
如: 以下有十个学生完成的脚本,现在需要将finish改为ing
[[email protected] test]# touch student_{01..10}_finish.sh [[email protected] test]# ls student_* student_01_finish.sh student_03_finish.sh student_05_f
student_02_finish.sh student_04_finish.sh student_06_finish.sh student_08_finish.sh student_10_finish.sh
方法1: sed
for f in `ls student_*` ; do echo $f | mv $f `sed ‘s#finish#ing#g‘`;done
方法2: awk 字符串拼接的方法(在传递给bash)
测试:
[[email protected] test]# echo student_05_finish.sh | awk -F ‘finish‘ ‘{ print "mv " $0 " " $1 "ing"$2}‘ mv student_05_finish.sh student_05_ing.sh
批量执行
[[email protected] test]# for f in `ls student*` ;do echo $f | awk -F ‘finish‘ ‘{ print "mv " $0 " " $1 "ing"$2}‘;done | bash [[email protected] test]# ls student_* student_01_ing.sh student_03_ing.sh student_05_ing.sh student_07_ing.sh student_09_ing.sh student_02_ing.sh student_04_ing.sh student_06_ing.sh student_08_ing.sh student_10_ing.sh
或:
for f in `ls student*` ;do echo $f | mv $f `awk -F ‘finish‘ ‘{ print $1 "ing" $2}‘`;done
方法三: rename命令,专业处理批量修改文件名的linux 命令
用法: man rename
[[email protected] test]# rename "finish" "ing" student_* [[email protected] test]# ls student_* student_01_ing.sh student_03_ing.sh student_05_ing.sh student_07_ing.sh student_09_ing.sh student_02_ing.sh student_04_ing.sh student_06_ing.sh student_08_ing.sh student_10_ing.sh
时间: 2024-10-14 18:42:14