批量修改文件名
创建素材
shuai@shuais-MacBook-Pro batchRename % touch image_{1..5}_game.jpgshuai@shuais-MacBook-Pro batchRename % touch image_{1..5}_name.pngshuai@shuais-MacBook-Pro batchRename % lsimage_1_game.jpg image_2_name.png image_4_game.jpg image_5_name.pngimage_1_name.png image_3_game.jpg image_4_name.pngimage_2_game.jpg image_3_name.png image_5_game.jpg
分析思路:第一步
对单个文件进行重命名
shuai@shuais-MacBook-Pro batchRename % mv image_1_game.jpg image_1.jpgshuai@shuais-MacBook-Pro batchRename % ls -ltotal 0-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5_name.png
第二步:子串匹配截取
利用 ${变量//pattern/string} 表示用string替代所有的pattern
shuai@shuais-MacBook-Pro batchRename % f=image_1_name.pngshuai@shuais-MacBook-Pro batchRename % echo ${f//_name/}image_1.png
第三步:echo 反引号+linux命令
# echo `linux命令` 可以输出命令的执行结果shuai@shuais-MacBook-Pro batchRename % echo `date`2022年 4月20日 星期三 22时57分02秒 CST# 使用mv和反引号加linux命令,动态修改名称shuai@shuais-MacBook-Pro batchRename % f=image_1_name.pngshuai@shuais-MacBook-Pro batchRename % mv $f `echo ${f//_name/}`shuai@shuais-MacBook-Pro batchRename % ls -ltotal 0-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5_game.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5_name.png
第四步:利用for循环
利用循环去掉剩下的所有jpg文件的_name字符
# `ls *game*jpg` 列出所有未处理过的jpgshuai@shuais-MacBook-Pro batchRename % for filename in `ls *game*jpg`;do echo $filename;doneimage_2_game.jpgimage_3_game.jpgimage_4_game.jpgimage_5_game.jpg
shuai@shuais-MacBook-Pro batchRename % for filename in `ls *game*jpg`;do mv $filename `echo ${filename//_game/}`;doneshuai@shuais-MacBook-Pro batchRename % ls -ltotal 0-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4_name.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5_name.png
shuai@shuais-MacBook-Pro batchRename % for filename in `ls *name*png`;do mv $filename `echo ${filename//_name/}`;doneshuai@shuais-MacBook-Pro batchRename % ls -ltotal 0-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_1.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_2.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_3.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_4.png-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5.jpg-rw-r--r-- 1 shuai staff 0 4 20 22:32 image_5.png
