Linux命令:convmv(文件名转码的工具)

convmv编码转换工具详解
有些情况下我们在window下上传到服务器时会出现编码不一致的情况如下:

convmv能帮助我们很容易地对一个文件,一个目录下所有文件进行编码转换,比如gbk转为utf8等。

安装 yum install convmv

语法:

convmv [options] FILE(S) … DIRECTORY(S)

主要选项:

1、-f ENCODING
指定目前文件名的编码,如-f gbk
2、-t ENCODING
指定将要转换成的编码,如-f utf-8
3、-r
递归转换目录下所有文件名
4、–list
列出所有支持的编码
5、–notest
默认是只打印转换后的效果,加这个选项才真正执行转换操作。

常用参数:

-r 递归处理子文件夹

–notest 真正进行操作,默认情况下是不对文件进行真实操作

–list 显示所有支持的编码

–unescap 可以做一下转义,比如把%20变成空格

-i 交互模式(询问每一个转换,防止误操作)

convmv 的使用方法:

convmv -f 源编码 -t 新编码 [选项] 文件名

例子:递归转换thesis目录(如下图)下的文件名编码gbk为utf-8:

convmv -f gbk -t utf-8 thesis

转换以后“GBK编码的文件名”会被转换成UTF-8编码(只是文件名编码的转换,文件内容不会发生变化)。

在这里插入图片描述

**注意:**不要在NTFS和FAT文件系统中使用此命令,否则可能产生意外,如果要在Linux中正确的显示NTFS和 FAT的中文件名,可以通过mount参数来解决,具体方法参考man手册。

其他详见:
https://blog.51cto.com/461205160/1729467
https://www.jb51.net/LINUXjishu/452416.html

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.
<think>嗯,用户遇到了Linux环境下中文文件名编码的问题,需要解决方案。首先,我得回忆一下相关的知识。Linux系统默认使用UTF-8编码,而Windows通常是GBK,这样跨系统传输文件时,如果编码不一致就会出现乱码。用户提供的引用里提到了convmv这个工具,可以用来转换文件名编码,应该是个关键点。 用户提到在CentOS 7.4下使用convmv,需要先用yum安装。convmv的用法应该包括指定原始编码和目标编码,比如从GBK转UTF-8。可能需要加上--notest参数实际执行转换,否则只是测试模式。记得提醒用户备份文件,防止操作失误。 另外,解压ZIP文件时出现乱码的问题,引用[2]提到了Java解压的处理方法,但用户可能是在Linux下直接解压。这时候可能需要使用unzip命令的-O参数指定编码,不过有些版本的unzip可能不支持这个选项。可能需要安装其他工具如unar,它支持自动检测编码,可能更方便。 还有查看和转换文件内容编码的情况,比如用iconv命令转换文件内容,或者用vim设置文件编码并保存。引用[3]里提到在vim中查看fileencoding,如果是latin1,可能需要手动转换为UTF-8。这部分可能需要指导用户如何操作。 然后,用户的问题是关于文件名编码的解决方案,需要分步骤说明。可能需要先检查系统语言环境,确认是否支持UTF-8。然后介绍使用convmv工具转换文件名,再处理解压时的编码问题,比如使用unar或者调整unzip参数。同时,可能还要提到设置环境变量,比如在解压时指定编码。 用户可能不太清楚各个工具的具体用法,所以需要给出具体的命令示例,比如convmv -f gbk -t utf-8 --notest -r .,并解释每个参数的作用。同时,提醒注意事项,比如备份数据,避免转换错误。 总结下来,解决方案应该包括以下几个步骤:安装必要工具,转换文件名编码,处理压缩文件,设置系统环境,检查文件内容编码。每个步骤都需要详细说明,并引用提供的参考资料作为支持。同时,生成相关问题时要围绕编码转换工具、解压乱码处理、系统配置等方面展开,帮助用户深入了解相关主题。</think>### Linux中文文件名编码问题解决方案 #### 1. 系统环境检查 首先确认系统是否使用UTF-8编码: ```bash echo $LANG ``` 若显示`en_US.UTF-8`或`zh_CN.UTF-8`则表示UTF-8环境。若显示`GBK`则需要修改环境变量[^3]。 #### 2. 文件名转码工具 使用`convmv`工具转换文件名编码: ```bash # 安装工具 yum install -y convmv # 测试转换效果(不实际执行) convmv -f gbk -t utf-8 -r /path/to/files/ # 确认无误后执行转换 convmv -f gbk -t utf-8 --notest -r /path/to/files/ ``` 参数说明: - `-f gbk`:原始编码 - `-t utf-8`:目标编码 - `-r`:递归处理目录 - `--notest`:实际执行转换[^1] #### 3. 压缩文件处理 解压ZIP文件时指定编码: ```bash unzip -O gbk filename.zip ``` 若系统不支持`-O`参数,可使用`unar`工具: ```bash yum install -y unar unar -e gbk filename.zip ``` #### 4. 文件内容编码转换 使用`iconv`转换文件内容编码: ```bash iconv -f gbk -t utf-8 input.txt -o output.txt ``` #### 5. Vim编码修正 在Vim中检查并转换单个文件: ```vim :set fileencoding=latin1 # 先按当前编码打开 :set fileencoding=utf-8 # 转换为UTF-8 :wq # 保存退出 ``` #### 注意事项 - 操作前建议备份重要文件 - 批量操作前先用`-r`参数测试 - 永久修改系统编码可编辑`/etc/locale.conf`
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值