若需要把目錄中的檔案從 GBK 轉換為 UTF-8,可以撰寫一個簡單的 shell 腳本,透過 iconv 批次處理。以下範例以 .gbk 副檔名為來源,轉換後產生對應的 UTF-8 檔案。
for srcfile in *.gbk
do
dstfile=${srcfile/.gbk/}
if [ "$srcfile" -nt "$dstfile" ]; then
echo convert $srcfile to $dstfile ...
iconv -f gbk -t utf8 $srcfile > $dstfile
fi
done腳本會逐一檢查來源檔案是否比目標檔案新,若是則執行轉換。這樣可避免重複處理尚未變更的檔案,提升批次轉換效率。
當有新檔案或修改時,會看到類似 convert sitemap.ftl.gbk to sitemap.ftl 的輸出;若沒有變更,則不會產生任何轉換訊息。