需求說明:
線上之前使用的圖片沒有經過處理,比較大。應產品同事要求,將圖片拿出來處理後再替換之前的圖片,圖片名不變。
/tmp/image目錄下存放的修改後的圖片
[root@kevin_web1 ~]# ls -l /tmp/image|head -10
total 66376 -rw-r--r-- 1 root root 114118 Jan 18 17:26 201407281934401406547280.jpg -rw-r--r-- 1 root root 110450 Jan 18 17:26 201407281934511406547291.jpg -rw-r--r-- 1 root root 156443 Jan 18 17:26 201407281937341406547454.jpg -rw-r--r-- 1 root root 89006 Jan 18 17:26 201407281938241406547504.jpg -rw-r--r-- 1 root root 136594 Jan 18 17:26 201407281938341406547514.jpg -rw-r--r-- 1 root root 74327 Jan 18 17:26 201407281938551406547535.jpg -rw-r--r-- 1 root root 114118 Jan 18 17:26 201407281940071406547607.jpg -rw-r--r-- 1 root root 110450 Jan 18 17:26 201407281940161406547616.jpg -rw-r--r-- 1 root root 105628 Jan 18 17:26 201407281940241406547624.jpg
線上圖片存放的路徑是/var/www/vhosts/www.kevin.com/main/upload/,修改後的圖片都有存放到這裡
比如:注意find命令中的目標目錄後面一定要加上/(比如下面的.../upload/,不能寫成.../upload,否則查詢不到)
[root@kevin_web1 ~]# find /var/www/vhosts/www.kevin.com/main/upload/ -name 201407281934401406547280.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281934401406547280.jpg [root@kevin_web1 ~]# find /var/www/vhosts/www.kevin.com/main/upload -name 201407281934401406547280.jpg [root@kevin_web1 ~]#
那麼修改後的圖片批量替換之前的圖片的指令碼如下:
#!/bin/bash for m in `ls -l /tmp/image|awk -F" " '{print $9}'` do for i in `cat /tmp/b.txt` do for n in `ls "$i"|awk -F"/" '{print $9}'` do if [ $m == $n ];then \cp -f /tmp/image/$m `cat /tmp/b.txt|grep "$m"` else echo "123" > /dev/null 2>&1 fi done done done
指令碼解刨如下:
[root@kevin_web1 ~]# ls -l /tmp/image|awk -F" " '{print $9}'|head -10
201407281934401406547280.jpg 201407281934511406547291.jpg 201407281937341406547454.jpg 201407281938241406547504.jpg 201407281938341406547514.jpg 201407281938551406547535.jpg 201407281940071406547607.jpg 201407281940161406547616.jpg 201407281940241406547624.jpg
[root@kevin_web1 ~]# for i in $(ls -l /tmp/image|awk -F" " '{print $9}');do echo `find /var/www/vhosts/www.kevin.com/main/upload/ -name "$i"` >> /tmp/b.txt;done
[root@kevin_web1 ~]# cat /tmp/b.txt |head -10
/var/www/vhosts/www.kevin.com/main/upload/20140728/201407281934401406547280.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281934511406547291.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281937341406547454.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281938241406547504.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281938341406547514.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281938551406547535.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281940071406547607.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281940161406547616.jpg /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281940241406547624.jpg /var/www/vhosts/www.kevin.com/main/upload/20140813/201408131047131407898033.jpg
[root@kevin_web1 ~]# ls /var/www/vhosts/www.kevin.com/main/upload/20140728/201407281934401406547280.jpg |awk -F"/" '{print $9}'
201407281934401406547280.jpg
[root@kevin_web1 ~]# cat /tmp/b.txt |grep 201407281934401406547280.jpg
/var/www/vhosts/www.kevin.com/main/upload/20140728/201407281934401406547280.jpg