该工具的功能是转换文件名的编码,也包括目录的转换,由于Linux系统中没有提供该工具,因此我们需要下载并且安装才可使用。
下载安装
安装convmv只需要两步,make和make install:
[root@mfsmaster ~]http://www.j3e.de/linux/convmv/convmv-1.14.tar.gz [root@mfsmaster convmv-1.14]# make pod2man --section 1 --center=" " convmv | gzip > convmv.1.gz [root@mfsmaster convmv-1.14]# make install pod2man --section 1 --center=" " convmv | gzip > convmv.1.gz mkdir -p /usr/local/share/man/man1/ mkdir -p /usr/local/bin/ cp convmv.1.gz /usr/local/share/man/man1/ install -m 755 convmv /usr/local/bin/
|
从最后一步看出,它只提供了一个命令,那就是convmv。
转换文件名编码
命令convmv的使用比较简单,下面是它的基本用法:
[root@mfsmaster ~]# convmv --help Your Perl version has fleas #22111 convmv 1.14 - converts filenames from one encoding to another Copyright (C) 2003-2008 Bjoern JACKE
This program comes with ABSOLUTELY NO WARRANTY; it may be copied or modified under the terms of the GNU General Public License version 2 or 3 as published by the Free Software Foundation.
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 --parsable write a parsable todo list (see man page) --help print this help |
使用convmv命令将现有的文件从GB2312转换为UTF-8:
[root@mfsmaster html]# convmv -f GB2312 -t UTF-8 --nosmart --notest ./*.* mv "./??.html" "./主页.html" Ready! [root@mfsmaster html]# ls 主页.html |
顺便说一句,它也可以转换目录的编码,例如:
[root@mfsmaster html]# ls ???? [root@mfsmaster mm]# convmv -f GB2312 -t UTF-8 --nosmart --notest ./* mv "./ü?" "./图片" Ready! [root@mfsmaster mm]# ls 图片 |
好了,现在我们访问该网页:
可见,不再是404了,至少文件找到了,可网页还是乱码,我们看一下在Linux系统中查看是什么情况:
[root@mfsmaster html]# more 主页.html
?o?????·??ginx£?Β?????£?£?£? |
同样是乱码,看一下Nginx服务器的配置:
location / { root /var/xx.com/html; index index.html index.htm; charset utf-8;
} |
这时我们有两种选择,第一将该网页内容也转换为UTF-8;第二重新设置Nginx的charset设置,就是将charset设置为gb2312。
我们先采取第一种方法将网页内容转换为UTF-8,这是需要一个工具enca,也许你会说Linux系统也带了一个了iconv命令。也可以将文件内容做编码转换,但是这个enca提供了两个命令一个是enca,另一个是enconv,通过enca可以先查看文件内容的编码,这是我们需要的,因此需要认识这个新的命令。
源地址:http://blog.itpub.net/27043155/viewspace-732155/