本文共 1439 字,大约阅读时间需要 4 分钟。
Linux中批量修改文件名
有时候我们有一个目录下面的文件名都需要重新命令
如: 以下有十个学生完成的脚本,现在需要将finish改为ing
1 2 3 | [root@slave test ] # touch student_{01..10}_finish.sh [root@slave test ] # ls student_* student_01_finish.sh student_03_finish.sh student_05_f |
1 | student_02_finish.sh student_04_finish.sh student_06_finish.sh student_08_finish.sh student_10_finish.sh |
方法1: sed
1 | for f in ` ls student_*` ; do echo $f | mv $f ` sed 's#finish#ing#g' `; done |
方法2: awk 字符串拼接的方法(在传递给bash)
测试:
1 2 | [root@slave test ] # echo student_05_finish.sh | awk -F 'finish' '{ print "mv " $0 " " $1 "ing"$2}' mv student_05_finish.sh student_05_ing.sh |
批量执行
1 2 3 4 | [root@slave test]# for f in `ls student*` ;do echo $f | awk -F 'finish' '{ print "mv " $0 " " $1 "ing"$2}';done | bash [root@slave 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 |
或:
1 | for f in ` ls student*` ; do echo $f | mv $f ` awk -F 'finish' '{ print $1 "ing" $2}' `; done |
方法三: rename命令,专业处理批量修改文件名的linux 命令
用法: man rename
1 2 3 4 | [root@slave test]# rename "finish" "ing" student_* [root@slave 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 |
本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1766448如需转载请自行联系原作者
cuizhiliang