Linux命令之convmv命令

这篇博客介绍了Linux系统中用于批量转换文件编码和文件名的convmv命令。从安装到使用,包括目录编码转换、文件名大小写转换、交互式确认等功能,详尽展示了convmv的用法。此外,还列举了可用的编码列表,并提供了参数说明,对于需要进行文件管理的用户非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、convmv命令简介

  单个文件编码转换我们可以使用系统自带的命令iconv,命令使用可以参考Linux命令之iconv命令。如果是整个中文文件夹及你们的文件全是乱码怎么办呢?这个时候我们可以使用convmv命令,通过-r参数完成整个目录里文件编码的转换。convmv能帮助我们很容易地对一个文件,一个目录下所有文件进行编码转换,比如gbk转为utf8等。convmv命令除了转换编码还可以转换文件名为大写或者小写,在需要批量更名大小写的时候非常有用。

二、使用示例

1、安装命令

[root@s145 tmp]# yum install convmv

2、获取命令帮助

[root@s145 tmp]# convmv --help
Your Perl version has fleas #22111 #37757 #49830
convmv 1.15 - converts filenames from one encoding to another
Copyright © 2003-2011 Bjoern JACKE bjoern@j3e.de

3、目录编码转换模拟

在这里插入图片描述

[root@s145 abc]# ll
total 0
drwxr-xr-x 2 root root 120 Mar 30 15:55 ϲ???ĸ?
[root@s145 abc]# convmv -f GBK -t UTF8 -r ./

4、目录编码转换真实执行

在这里插入图片描述

[root@s145 abc]# convmv -f GBK -t UTF8 -r --notest ./
mv “./ϲ.mp3” “./ϲ - 佛系少女.mp3”
mv “./ϲ - .mp3” “./ϲ风吹麦浪.mp3”
mv “./ϲ- ŭ.mp3” “./ϲ怒放的生命.mp3”
mv “./ϲ.mp3” “./ϲ - 因为有你.mp3”
mv “./ϲ”./喜欢的歌"
Ready!

5、列出所执行的编码

#所支持的编码比iconv命令少,但是也有100+种编码,完全覆盖了常见编码类型。
[root@s145 abc]# convmv --list
7bit-jis
AdobeStandardEncoding
AdobeSymbol
AdobeZdingbat
ascii
ascii-ctrl
big5-eten
big5-hkscs

utf-8-strict
utf8
viscii
[root@s145 abc]# convmv --list |wc -l
124

6、将文件名转换为大写

在这里插入图片描述

#注意此修改是将文件后缀大小写一并修改
[root@s145 tmp]# convmv --upper abc2
Starting a dry run without changes…
mv “./abc2” “./ABC2”
No changes to your files done. Use --notest to finally rename the files.

7、将文件名转换为小写

在这里插入图片描述

#注意此修改是将文件后缀大小写一并修改
[root@s145 tmp]# convmv --lower -r --notest ABC2
mv “ABC2/B.csv” “ABC2/b.csv”
mv “ABC2/B.CSV” “ABC2/b.csv”
b.csv exists and differs or --replace option missing - skipped
mv “ABC2/C.CSV” “ABC2/c.csv”
mv “ABC2/D.CSV” “ABC2/d.csv”
mv “ABC2/E.CSV” “ABC2/e.csv”
mv “./ABC2” “./abc2”
Ready!
[root@s145 tmp]# ll abc2
total 20
-rw-r–r-- 1 root root 725 Mar 24 09:51 b.csv
-rw-r–r-- 1 root root 775 Mar 24 13:37 B.CSV
-rw-r–r-- 1 root root 725 Mar 24 10:12 c.csv
-rw-r–r-- 1 root root 725 Mar 24 10:14 d.csv
-rw-r–r-- 1 root root 775 Mar 24 10:21 e.csv

8、转换前交互式确认是否执行

在这里插入图片描述

#使用-i参数执行交互式确认,选择y执行,选择n表示不执行转换。
[root@s145 abc]# convmv -f utf8 -t GBK -r --notest -i ./
mv “./喜欢的歌/冯提莫 - 佛系少女.mp3” “./喜欢的歌/Ī - .mp3” (y/n) y

三、使用语法及参数说明

1、使用语法

用法:#convmv [options] 文件/目录

2、常用参数说明

参数参数说明
-f enc源编码
-t enc新编码
-r递归处理子文件夹
-i交互文向转换,每次转换前询问
–list显示所有可用编码
–nosmart如果是utf8文件,忽略
–notest直接转换不测试
–replace文件相同直接替换
–unescape可以做一下转义,比如把%20变成空格
–upper全部转换成大写
–lower全部转换成小定
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恒悦sunsite

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值