今天有几张后缀为jpg的图片需要批量修改名称
首先查看下所有的名称:
-rwxrwxrwx 1 michael michael 0 Jan 15 14:21 goo01 -rwxrwxrwx 1 michael michael 0 Jan 15 14:23 goo02 -rwxrwxrwx 1 michael michael 0 Jan 15 14:23 goo03 -rwxrwxrwx 1 michael michael 431898 Jan 15 10:39 '满意度 2020-01-15_10.jpg' -rwxrwxrwx 1 michael michael 431613 Jan 15 10:39 '满意度 2020-01-15_11.jpg' -rwxrwxrwx 1 michael michael 448659 Jan 15 10:39 '满意度 2020-01-15_12.jpg' -rwxrwxrwx 1 michael michael 430719 Jan 15 10:39 '满意度 2020-01-15_13.jpg' -rwxrwxrwx 1 michael michael 459284 Jan 15 10:39 '满意度 2020-01-15_14.jpg' -rwxrwxrwx 1 michael michael 453341 Jan 15 10:39 '满意度 2020-01-15_2.jpg' -rwxrwxrwx 1 michael michael 428014 Jan 15 10:39 '满意度 2020-01-15_3.jpg'
因为在windows10下面,现实的名称是很多都是带着空格的,名称显示的时候会有单引号
如果通过for循坏去批量修改的话会出现以下这个情况,满意度和2020-01-15_10.jpg变成了两行
for i in `ls *`; do echo $i; done goo01 goo02 goo03 满意度 2020-01-15_10.jpg 满意度 2020-01-15_11.jpg 满意度 2020-01-15_12.jpg 满意度 2020-01-15_13.jpg 满意度 2020-01-15_14.jpg 满意度
针对这个情况可以使用read line的方式批量操作
$ ls *|while read i; do echo $i; done goo01 goo02 goo03 满意度 2020-01-15_10.jpg 满意度 2020-01-15_11.jpg 满意度 2020-01-15_12.jpg 满意度 2020-01-15_13.jpg 满意度 2020-01-15_14.jpg
然后通过mv、echo、sed来批量修改,这里要注意,所有的文件引用时需要把引号带上,我们尝试将所有“满意度”改为“test”
ls *|while read i; do mv \'$i\' \'`echo $i |sed "s#满意度#test#g"`\'; do ne mv: cannot stat "'goo01'": No such file or directory mv: cannot stat "'goo02'": No such file or directory mv: cannot stat "'goo03'": No such file or directory mv: target "2020-01-15_10.jpg'" is not a directory mv: target "2020-01-15_11.jpg'" is not a directory mv: target "2020-01-15_12.jpg'" is not a directory mv: target "2020-01-15_13.jpg'" is not a directory
但是你会发现,报错了,提示没有这个文件,在windows10就是麻烦,但是这个时候你可以通过eval来处理这个情况
ls *|while read i; do eval mv \'$i\' \'`echo $i |sed "s#满意度#test#g"`\ '; done mv: 'goo01' and 'goo01' are the same file mv: 'goo02' and 'goo02' are the same file mv: 'goo03' and 'goo03' are the same file mv: '签到表.jpg' and '签到表.jpg' are the same file
这边提示相同的文件不是我们要修改的内容,我们这边只是测试将“签到表”改为“test”,让我们查看一下,所有的文件已经批量修改完毕了
drwxrwxrwx 1 michael michael 4096 Jan 15 14:39 ./ drwxrwxrwx 1 michael michael 4096 Jan 15 14:14 ../ -rwxrwxrwx 1 michael michael 0 Jan 15 14:21 goo01* -rwxrwxrwx 1 michael michael 0 Jan 15 14:23 goo02* -rwxrwxrwx 1 michael michael 0 Jan 15 14:23 goo03* -rwxrwxrwx 1 michael michael 431898 Jan 15 10:39 'test 2020-01-15_10.jpg'* -rwxrwxrwx 1 michael michael 431613 Jan 15 10:39 'test 2020-01-15_11.jpg'* -rwxrwxrwx 1 michael michael 448659 Jan 15 10:39 'test 2020-01-15_12.jpg'* -rwxrwxrwx 1 michael michael 430719 Jan 15 10:39 'test 2020-01-15_13.jpg'* -rwxrwxrwx 1 michael michael 459284 Jan 15 10:39 'test 2020-01-15_14.jpg'*
eval可读取一连串的参数,然后再依参数本身的特性来执行。
linux的eval命令可以参考这个教程:https://www.runoob.com/linux/linux-comm-eval.html