Linux對中文的支援不是很好,
也不像Windows樣,會對檔案名,檔案内容做字元集的自動轉換。
例如,将Windows下的檔案複制到Linux下,會出現一堆的亂碼,
這時,就要用到linux的一些字元集轉換工具來處理。
1. 批量檔案名字元集轉換工具 -- convmv
下載下傳連結:
http://download.chinaunix.net/download/0002000/1760.shtml
convmv是一個更改檔案名編碼方式的工具,
它甚而可以進行目錄下檔案名的批量轉換。
例如,
将/home目錄下原來檔案名是gbk編碼方式的全部改為utf-8格式的,
使用指令如下:
- $./convmv -f gbk -t utf8 -r –notest /home
-f 檔案名原編碼方式;
-t 檔案名要更改為的編碼方式;
-r 這個目錄下面的所有檔案;
-notest 表示馬上執行,而不是僅僅測試而已。
更多的功能詳解可見它的幫助:
- USAGE: convmv [options] FILE(S)
- -f enc encoding *from* which should be converted
- -t enc encoding *to* which should be converted
- -r recursively go through directories
- -i interactive mode (ask for each action)
- --nfc target files will be normalization form C for UTF-8 (Linux etc.)
- --nfd target files will be normalization form D for UTF-8 (OS X etc.)
- --qfrom be quiet about the "from" of a rename (if it screws up your terminal e.g.)
- --qto be quiet about the "to" of a rename (if it screws up your terminal e.g.)
- --exec c execute command instead of rename (use #1 and #2 and see man page)
- --list list all available encodings
- --lowmem keep memory footprint low (see man page)
- --nosmart ignore if files already seem to be UTF-8 and convert if posible
- --notest actually do rename the files
- --replace will replace files if they are equal
- --unescape convert%20ugly%20escape%20sequences
- --upper turn to upper case
- --lower turn to lower case
- --help print this help
2. 檔案内容字元集轉換工具 -- iconv
它通常是Linux系統自帶的轉換工具。
- $iconv -f gbk -t utf8 -o outfile infile
-f 檔案原來的編碼方式;
-t 輸出檔案的編碼方式;
-o 輸出檔案名,這利用outfile表示,
最後跟上要更改編碼方式的檔案名infile
這個工具有兩個缺陷:
一個是必須先知道檔案原來的編碼方式, 否則将出錯。
這個可以用指令 "file filename" 得到。
二個是如果轉換出錯,将不會傳回。
iconv的用法:
- iconv指令用于轉換指定檔案的編碼,預設輸出到标準輸出裝置,亦可指定輸出檔案。
- 用法:iconv [選項...] [檔案...]
- 有如下選項可用:
- 輸入/輸出格式規範:
- -f, --from-code=名稱 原始文本編碼
- -t, --to-code=名稱 輸出編碼
- 資訊:
- -l, --list 列舉所有已知的字元集
- 輸出控制:
- -c 從輸出中忽略無效的字元
- -o, --output=FILE 輸出檔案
- -s, --silent 關閉警告
- --verbose 列印進度資訊
- --help 給出該系統求助清單
- --usage 給出簡要的用法資訊
- -V, --version 列印程式版本号
3. 強大的檔案内容字元集轉換工具 -- enca
下載下傳位址:
- $wget http://dl.cihar.com/enca/enca-1.13.tar.gz
在應用上enca比iconv更完善,在中文支援上enca比iconv支援得好,
iconv當遇到不支援的中文時會跳過或者報錯cannot iconving。
是以推薦用enca。
編譯與安裝:
- $tar -jxvf enca-1.13
- $cd enca-1.13
- $./configure
- $make
- $make check
- $make install
編譯時的出錯解決:
如果編譯時遇到出錯提示:
- ...
- /usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/libm.a(w_exp.o):
- relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object;
- recompile with -fPIC
- /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/libm.a: could not read symbols:
- Bad value
- collect2: ld returned 1 exit status
- make[2]: *** [libenca.la] Error 1
則需要更改它的配置選項為:
- $./configure --enable-shared=no
原因詳解:
http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3
使用示例:
- enca -L zh_CN test.sql // 檢查檔案編碼