歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 如何在LINUX中修改文件字符集

如何在LINUX中修改文件字符集

日期:2017/3/3 16:12:20   编辑:關於Linux

有些情況下,我們需要修改文件的字符集,以便解決亂碼或者其他問題。在linux下,操作系統為我們提供了ICONV這個命令,下面我們來看一下這個命令的具體使用方法。

[root@oadata ~]# iconv --help  
用法: iconv [選項...] [文件...]  
轉換給定文件的編碼。  
      
 輸入/輸出格式規范:  
  -f, --from-code=名稱     原始文本編碼  
  -t, --to-code=名稱       輸出編碼  
      
 信息:  
  -l, --list                 列舉所有已知的字符集  
      
 輸出控制:  
  -c                         從輸出中忽略無效的字符  
  -o, --output=FILE          輸出文件  
  -s, --silent               關閉警告  
      --verbose              打印進度信息  
      
  -?, --help                 給出該系統求助列表  
      --usage                給出簡要的用法信息  
  -V, --version              打印程序版本號  
      
Mandatory or optional arguments to long options are also mandatory or optional  
for any corresponding short options.  
      
For bug reporting instructions, please see:  
<http://www.gnu.org/software/libc/bugs.html>.

如果要查看某個文件的字符信息,linux同樣提供了file函數:

[root@oadata ~]# file --help  
Usage: file [OPTION]... [FILE]...  
Determine file type of FILEs.  
      
  -m, --magic-file LIST      use LIST as a colon-separated list of magic  
                               number files  
  -z, --uncompress           try to look inside compressed files  
  -b, --brief                do not prepend filenames to output lines  
  -c, --checking-printout    print the parsed form of the magic file, use in  
                               conjunction with -m to debug a new magic file  
                               before installing it  
  -f, --files-from FILE      read the filenames to be examined from FILE  
  -F, --separator string     use string as separator instead of `:'  
  -i, --mime                 output mime type strings  
  -k, --keep-going           don't stop at the first match  
  -L, --dereference          causes symlinks to be followed  
  -n, --no-buffer            do not buffer output  
  -N, --no-pad               do not pad output  
  -p, --preserve-date        preserve access times on files  
  -r, --raw                  don't translate unprintable chars to \ooo  
  -s, --special-files        treat special (block/char devices) files as  
                             ordinary ones  
      --help                 display this help and exit  
      --version              output version information and exit  
      
[root@oadata ~]# file -i a.log  
a.log: text/plain; charset=us-ascii

單文件的字符集轉換:

[oracle@oadata dir1]$ ls -l  
總計 4  
-rw-r--r-- 1 oracle oinstall 22 10-12 13:14 utf8.txt  
[oracle@oadata dir1]$ file -i utf8.txt   
utf8.txt: text/plain; charset=utf-8  
[oracle@oadata dir1]$ iconv -f utf-8 -t gbk -o gbk.txt utf8.txt   
[oracle@oadata dir1]$ ls -l  
總計 8  
-rw-r--r-- 1 oracle oinstall 15 10-12 13:43 gbk.txt  
-rw-r--r-- 1 oracle oinstall 22 10-12 13:14 utf8.txt  
[oracle@oadata dir1]$ file -i gbk.txt   
gbk.txt: text/plain; charset=iso-8859-1

對某目錄下的文件進行字符集轉換:

<pre name="code" class="plain">for i in `find ./ -name *.php` ;   
do echo $i;iconv -c -f gb18030 -t utf8 $i -o /tmp/iconv.tmp;  
mv /tmp/iconv.tmp $i;   
done</pre><br><br>

 

Copyright © Linux教程網 All Rights Reserved