
我正在嘗試替換某些文件名中不帶連字符的 guid。
我認為我已經完成了正規表示式,但是我似乎無法獲得正確的轉義或替換命令來相互協作。
這是我的正規表示式
https://regex101.com/r/SiqsjP/1
(-[0-9a-f]{32})
檔案名稱像這樣
iPhone6Plus-learn_multi_child-0dfb2dc71fe20da66ca47190d3136b12.png
我看過這個答案用於尋找和刪除檔案名稱中的子字串的 Bash shell 腳本但又不太一樣...
我認為這應該可行,但它不會抱怨錯誤?
newname=`echo "$filename" | sed -e 's/\([0-9a-f]{32}\)\.png/\1.png/'`
答案1
在sed 布雷(基本正規表示式)您還應該轉義大括號{}
:
newname=`echo "$filename" | sed 's/-[0-9a-f]\{32\}//g'`
要移動/重新命名檔案:
mv "$filename" "$newname"
答案2
在 shell 中使用字串操作:
for name in *.png; do
# remove everything after the last '-' including the dash
# and add the '.png' extension back
newname="${name%-*}.png"
echo mv "$name" "$newname"
done
這假設您要重新命名的所有檔案都是.png
目前目錄中的檔案。
運行一次並刪除(echo
如果它看起來似乎在做正確的事情)。