linux批量重命名方法
28 June 2010
经常会遇到需要批量重命名文件的情况,用鼠标太夸张了,简单总结两种比较简单的方法:
问题:
存在大量html文件,需要重命名为htm文件
1.rename
通常重命名的命令是mv,Larry Wall写的rename具有更强大的功能。能够用perl的正则语法结构进行文件重命名。
NAME rename - renames multiple files SYNOPSIS rename [ -v ] [ -n ] [ -f ] perlexpr [ files ] DESCRIPTION "rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input. For example, to rename all files matching "*.bak" to strip the extension, you might say rename 's//.bak$//' *.bak To translate uppercase names to lower, you'd use rename 'y/A-Z/a-z/' *
上面是rename的man手册,针对上面重命名html文件的问题,解决如下:
rename 's/.html$/.htm/' *.html
简单来说就是重命名所有html文件(*.html),将文件名结尾处.html替换为.htm('s/.html$/.htm/'),其实更简单的表达式可以为's/l$//'
2.printf
使用shell命令中的prinf打印出需要的文件名,然后在使用mv重命名
for i in *.html
do
newname="`printf "%s" ${i/html}`"htm
mv $i $newname
done
blog comments powered by Disqus