linux convmv

   该工具的功能是转换文件名的编码,也包括目录的转换,由于Linux系统中没有提供该工具,因此我们需要下载并且安装才可使用。

 

下载安装

 

    安装convmv只需要两步,makemake 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;第二重新设置Nginxcharset设置,就是将charset设置为gb2312

 

我们先采取第一种方法将网页内容转换为UTF-8,这是需要一个工具enca,也许你会说Linux系统也带了一个了iconv命令。也可以将文件内容做编码转换,但是这个enca提供了两个命令一个是enca,另一个是enconv,通过enca可以先查看文件内容的编码,这是我们需要的,因此需要认识这个新的命令。

源地址:http://blog.itpub.net/27043155/viewspace-732155/ 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NAME SYNOPSIS OPTIONS DESCRIPTION Filesystem issues HFS+ on OS X / Darwin JFS NFS4 FAT/VFAT and NTFS How to undo double UTF-8 (or other) encoded filenames How to repair Samba files Netatalk interoperability issues SEE ALSO BUGS AUTHOR NAME convmv - converts filenames from one encoding to another SYNOPSIS convmv [options] FILE(S) ... DIRECTORY(S) OPTIONS -f ENCODING specify the current encoding of the filename(s) from which should be converted -t ENCODING specify the encoding to which the filename(s) should be converted -i interactive mode (ask y/n for each action) -r recursively go through directories --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 , --qto be more quiet about the "from" or "to" of a rename (if it screws up your terminal e.g.). This will in fact do nothing else than replace any non-ASCII character (bytewise) with ? and any control character with * on printout, this does not affect rename operation itself. --exec command execute the given command. You have to quote the command and #1 will be substituted by the old, #2 by the new filename. Using this option link targets will stay untouched. Example: convmv -f latin1 -t utf-8 -r --exec "echo #1 should be renamed to #2" path/to/files --list list all available encodings. To get support for more Chinese or Japanese encodings install the Perl HanExtra or JIS2K Encode packages. --lowmem keep memory footprint low by not creating a hash of all files. This disables checking if symlink targets are in subtree. Symlink target pointers will be converted regardlessly. If you convert multiple hundredthousands or millions of files the memory usage of convmv might grow quite high. This option would help you out in that case. --nosmart by default convmv will detect if a filename is already UTF8 encoded and will skip this file if conversion from some charset to UTF8 should be performed. --nosmart will also force conversion to UTF-8 for such files, which might result in "double encoded UTF-8" (see section below). --fixdouble using the --fixdouble option convmv does only convert files which will still be UTF-8 encoded after conversion. That's useful for fixing double-encoded UTF-8 files. All files which are not UTF-8 or will not result in UTF-8 after conversion will not be touched. Also see chapter "How to undo double UTF-8 ..." below. --notest Needed to actually rename the files. By default convmv will just print what it wants to do. --parsable This is an advanced option that people who want to write a GUI front end will find useful (some others maybe, too). It will convmv make print out what it would do in an easy parsable way. The first column contains the action or some kind of information, the second column mostly contains the file that is to be modified and if appropriate the third column contains the modified value. Each column is separated by \0\n (nullbyte newline). Each row (one action) is separated by \0\0\n (nullbyte nullbyte newline). --preserve-mtimes modifying filenames usually causes the parent directory's mtime being updated. This option allows to reset the mtime to the old value. If your filesystem supports sub-second resolution the sub-second part of the atime and mtime will be lost as Perl does not yet support that. --replace if the file to which shall be renamed already exists, it will be overwritten if the other file content is equal. --unescape this option will remove this ugly % hex sequences from filenames and turn them into (hopefully) nicer 8-bit characters. After --unescape you might want to do a charset conversion. This sequences like etc. are sometimes produced when downloading via http or ftp. --upper , --lower turn filenames into all upper or all lower case. When the file is not ASCII-encoded, convmv expects a charset to be entered via the -f switch. --dotlessi care about the dotless i/I issue. A lowercase version of "I" will also be dotless while an uppercase version of "i" will also be dotted. This is an issue for Turkish and Azeri. By the way: The superscript dot of the letter i was added in the Middle Ages to distinguish the letter (in manuscripts) from adjacent vertical strokes in such letters as u, m, and n. J is a variant form of i which emerged at this time and subsequently became a separate letter. --help print a short summary of available options --dump-options print a list of all available options DESCRIPTION convmv is meant to help convert a single filename, a directory tree and the contained files or a whole filesystem into a different encoding. It just converts the filenames, not the content of the files. A special feature of convmv is that it also takes care of symlinks, also converts the symlink target pointer in case the symlink target is being converted, too. All this comes in very handy when one wants to switch over from old 8-bit locales to UTF-8 locales. It is also possible to convert directories to UTF-8 which are already partly UTF-8 encoded. convmv is able to detect if certain files are UTF-8 encoded and will skip them by default. To turn this smartness off use the --nosmart switch. Filesystem issues Almost all POSIX filesystems do not care about how filenames are encoded, here are some exceptions: HFS+ on OS X / Darwin Linux and (most?) other Unix-like operating systems use the so called normalization form C (NFC) for its UTF-8 encoding by default but do not enforce this. Darwin, the base of the Macintosh OS enforces normalization form D (NFD), where a few characters are encoded in a different way. On OS X it's not possible to create NFC UTF-8 filenames because this is prevented at filesystem layer. On HFS+ filenames are internally stored in UTF-16 and when converted back to UTF-8, for the underlying BSD system to be handable, NFD is created. See http://developer.apple.com/qa/qa2001/qa1173.html for defails. I think it was a very bad idea and breaks many things under OS X which expect a normal POSIX conforming system. Anywhere else convmv is able to convert files from NFC to NFD or vice versa which makes interoperability with such systems a lot easier. JFS If people mount JFS partitions with iocharset=utf8, there is a similar problem, because JFS is designed to store filenames internally in UTF-16, too; that is because Linux' JFS is really JFS2, which was a rewrite of JFS for OS/2. JFS partitions should always be mounted with iocharset=iso8859-1, which is also the default with recent 2.6.6 kernels. If this is not done, JFS does not behave like a POSIX filesystem and it might happen that certain files cannot be created at all, for example filenames in ISO-8859-1 encoding. Only when interoperation with OS/2 is needed iocharset should be set according to your used locale charmap. NFS4 Despite other POSIX filesystems RFC3530 (NFS 4) mandates UTF-8 but also says: "The nfs4_cs_prep profile does not specify a normalization form. A later revision of this specification may specify a particular normalization form." In other words, if you want to use NFS4 you might find the conversion and normalization features of convmv quite useful. FAT/VFAT and NTFS NTFS and VFAT (for long filenames) use UTF-16 internally to store filenames. You should not need to convert filenames if you mount one of those filesystems. Use appropriate mount options instead! How to undo double UTF-8 (or other) encoded filenames Sometimes it might happen that you "double-encoded" certain filenames, for example the file names already were UTF-8 encoded and you accidently did another conversion from some charset to UTF-8. You can simply undo that by converting that the other way round. The from-charset has to be UTF-8 and the to-charset has to be the from-charset you previously accidently used. If you use the --fixdouble option convmv will make sure that only files will be processed that will still be UTF-8 encoded after conversion and it will leave non-UTF-8 files untouched. You should check to get the correct results by doing the conversion without --notest before, also the --qfrom option might be helpful, because the double utf-8 file names might screw up your terminal if they are being printed - they often contain control sequences which do funny things with your terminal window. If you are not sure about the charset which was accidently converted from, using --qfrom is a good way to fiddle out the required encoding without destroying the file names finally. How to repair Samba files When in the smb.conf (of Samba 2.x) there hasn't been set a correct "character set" variable, files which are created from Win* clients are being created in the client's codepage, e.g. cp850 for western european languages. As a result of that the files which contain non-ASCII characters are screwed up if you "ls" them on the Unix server. If you change the "character set" variable afterwards to iso8859-1, newly created files are okay, but the old files are still screwed up in the Windows encoding. In this case convmv can also be used to convert the old Samba-shared files from cp850 to iso8859-1. By the way: Samba 3.x finally maps to UTF-8 filenames by default, so also when you migrate from Samba 2 to Samba 3 you might have to convert your file names. Netatalk interoperability issues When Netatalk is being switched to UTF-8 which is supported in version 2 then it is NOT sufficient to rename the file names. There needs to be done more. See http://netatalk.sourceforge.net/2.0/htmldocs/upgrade.html#volumes-and-filenames and the uniconv utility of Netatalk for details. SEE ALSO locale(1) utf-8(7) charsets(7) BUGS no bugs or fleas known AUTHOR Bjoern JACKE Send mail to bjoern [at] j3e.de for bug reports and suggestions.
### 回答1: 当Linux文件名出现乱码时,可能是因为文件名中包含了非ASCII字符,而系统默认使用的字符编码不支持这些字符。解决方法可以尝试以下几种: 1. 修改系统字符编码:可以通过修改系统的字符编码来解决乱码问题。具体方法可以参考Linux系统的相关文档。 2. 修改文件名:将文件名中的非ASCII字符替换为系统支持的字符,或者使用Unicode编码的字符来命名文件。 3. 使用特定的字符编码打开文件:如果文件名中包含了非ASCII字符,可以尝试使用特定的字符编码打开文件,比如UTF-8编码。 4. 使用特定的工具:有些特定的工具可以处理乱码问题,比如convmv命令可以将文件名从一种字符编码转换为另一种字符编码。 ### 回答2: Linux文件名乱码可能与字符编码不匹配有关。在Linux系统中,默认使用的是UTF-8编码,该编码能够支持包括中文在内的绝大部分字符。因此,应确保系统的字符编码正确设置。 如果在Linux中看到文件名乱码,可以尝试以下解决方法: 1. 检查系统字符编码设置:使用命令`locale`查看当前的字符编码设置,确保其为UTF-8。 2. 修改终端字符编码:如果使用终端命令行操作时出现文件名乱码,可以尝试修改终端的字符编码设置。可以使用`export LANG="en_US.UTF-8"`或`export LC_ALL="en_US.UTF-8"`命令来设置终端的字符编码为UTF-8。 3. 文件名编码转换:如果文件名已经乱码,可以使用`convmv`命令将文件名转换为正确的编码格式。例如,使用`convmv -r -f old_encoding -t UTF-8 --notest <filename>`命令来转换文件名编码。 4. 使用正确的字符编码打开文件:如果文件名仍然无法显示正确,可能是文件本身的编码问题。以文本编辑器打开文件时,需要确保使用正确的字符编码来解析文件内容。 综上所述,解决Linux文件名乱码问题可以通过检查和修改系统字符编码、终端字符编码设置,以及进行文件名编码转换等方式。 ### 回答3: 当我们在Linux系统中遇到文件名乱码时,通常是由于字符集编码问题导致的。Linux系统支持多种字符集编码,比如UTF-8、GBK等,而不同的字符集编码可能导致文件名显示不正确。以下是一些可能的解决方法: 1. 查看当前字符集编码:可以使用 `locale` 命令来查看当前系统的字符集编码设置。确保字符集编码设置正确并支持所需字符。 2. 更改字符集编码:如果当前字符集编码设置不正确,可以使用 `locale-gen` 命令重新生成并更改字符集编码。可以在 `/etc/locale.gen` 文件中进行相应配置。 3. 重命名文件:对于已经存在乱码的文件,可以使用 `mv` 命令重命名文件为正确的字符集编码。例如,`mv 乱码文件名 正确文件名`。需要注意的是,需要确保正确的字符集编码与文件实际编码一致。 4. 修改Shell环境:有时候,在Shell配置文件中添加以下语句可以解决字符集编码问题: ``` export LANG=en_US.UTF-8 export LC_CTYPE=en_US.UTF-8 ``` 5. 使用字符转换工具:如果有大批量乱码的文件需要处理,可以使用一些字符转换工具,如 `iconv` 命令,进行批量转换文件名字符集编码。 总之,解决Linux文件名乱码问题的关键在于正确设置字符集编码,并确保字符集编码与文件实际编码一致。通过一些基本的命令和工具,我们可以简单地解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值