利用 tr 搭配 shell 的字串處理,就能批量把檔名轉成大寫。核心技巧是先用 ${i%%.*} 取檔名、${i#*.} 取副檔名,再加上 if 判斷,只處理符合條件的檔案,避免重複處理。
只把純小寫字母構成的檔名轉大寫:
for i in * ; do exp=${i#*.};o=${i%%.*}; if [[ $o =~ ^[a-z]+$ ]];then mv $i $(echo $o|tr [a-z] [A-Z]).$exp; fi; done
連副檔名一起轉大寫:
for i in * ; do exp=${i#*.};o=${i%%.*}; if [[ $o =~ ^[a-z]+$ ]];then mv $i $(echo $o|tr [a-z] [A-Z]).$(echo $exp|tr [a-z] [A-Z]); fi; done
只要檔名以小寫字母開頭就處理:
for i in * ; do exp=${i#*.};o=${i%%.*}; if [[ $o =~ ^[a-z] ]];then mv $i $(echo $o|tr [a-z] [A-Z]).$exp; fi; done
同樣地,連副檔名一起轉:
for i in * ; do exp=${i#*.};o=${i%%.*}; if [[ $o =~ ^[a-z] ]];then mv $i $(echo $o|tr [a-z] [A-Z]).$(echo $exp|tr [a-z] [A-Z]); fi; done
只要調整 if 的正則條件,就能靈活決定要轉換哪些檔案。