背景
使用U盤往Windows主機、Linux主機傳文件還是經常的事,但有時候文件名有中文,
傳到Linux機器會有亂碼,選擇起來也很麻煩,最近剛好遇到,寫下解決方法。
環境
Linux
[root@min-base ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@min-base ~]# echo $LANG en_US.UTF-8 [root@min-base ~]#
Windows XShell
問題
txt文件是用Xftp直接從Windows上拖過來的,在Linux機上顯示亂碼了。
[root@min-base ~]# ll total 801272 -rw-------. 1 root root 996 Dec 15 15:56 anaconda-ks.cfg drwxr-xr-x. 6 root root 48 Dec 21 07:13 cmake-3.7.1-Linux-x86_64 -rw-r--r--. 1 root root 30679418 Dec 21 07:13 cmake-3.7.1-Linux-x86_64.tar.gz drwxr-xr-x. 4 root root 29 Dec 21 06:09 llvm-src -rw-r--r--. 1 root root 779956643 Jan 1 17:40 llvm-src.tar.gz drwxr-xr-x. 8 1000 1000 4096 Aug 10 2013 vim74 -rw-r--r--. 1 root root 9843297 Dec 21 05:04 vim-7.4.tar.bz2 -rw-r--r--. 1 root root 975 Jan 10 06:01 ??????ϰregexp.txt [root@min-base ~]#
解決
一般這種問題是文件的編碼字符集、Shell編碼字符集、XShell編碼字符集不匹配,設置匹配就可以了。
臨時辦法
1)簡體中文的Windows一般使用GB字符集,這里將XShell設置為GBK
2)
Linux主機
[root@min-base ~]# export LANG=zh_CN.gbk
再試,就可以正常顯示中文了。
[root@min-base ~]# ll 總用量 801272 -rw-------. 1 root root 996 12月 15 15:56 anaconda-ks.cfg drwxr-xr-x. 6 root root 48 12月 21 07:13 cmake-3.7.1-Linux-x86_64 -rw-r--r--. 1 root root 30679418 12月 21 07:13 cmake-3.7.1-Linux-x86_64.tar.gz drwxr-xr-x. 4 root root 29 12月 21 06:09 llvm-src -rw-r--r--. 1 root root 779956643 1月 1 17:40 llvm-src.tar.gz drwxr-xr-x. 8 1000 1000 4096 8月 10 2013 vim74 -rw-r--r--. 1 root root 9843297 12月 21 05:04 vim-7.4.tar.bz2 -rw-r--r--. 1 root root 975 1月 10 06:01 正則練習regexp.txt
不過以上設置只對當前shell連接生效,新開的shell還是用的原來的設置。
永久生效
1) XShell屬性設置
2) Linux環境變量設置
[root@min-base ~]# vim /etc/profile
在末尾追加
export LANG=zh_CN.gbk
就可以了。
然而。。。。。。我發現對插在Linux機器上的U盤仍然不能正確識別出中文目錄、中文文件
原來另有蹊蹺,就是在掛載U盤的時候設置下字符集
[root@min-base ~]# mount -o iocharset=gbk /dev/sdb4 /mnt
這樣設置一般差不多就可以解決了。
不過此操作引發了導入xxx.sql文件時數據庫中文亂碼問題,有待研究...