批量重命名文件名

来源:互联网 发布:linux ftp和vsftp 编辑:程序博客网 时间:2024/05/17 01:27

批量重命名文件名是常见的需求。例如我们这有三个文件都是以.txt结尾,现在我们想重命名为.csv文件。以下给同学们介绍如下两种方式。

1.使用mv命令

话不多少,直接上代码

#!/bin/bashfor file in `ls`do    newfile=`echo $file | sed 's/\.txt/\.csv/'`    mv $file $newfiledone

mv的方式如上。如果不是修改后缀而是其他方式,修改后面sed的正则表达方式即可。

2.使用rename命令

rename .txt .csv *

rename 命令可能实际中用得不如mv多。rename最大的好处就是可以同时处理多个文件。来看看rename的用法:

RENAME(1)                  Linux Programmer’s Manual                 RENAME(1)NAME       rename - Rename filesSYNOPSIS       rename from to file...       rename -VDESCRIPTION       rename will rename the specified files by replacing the first occurrence of from in their name by to.       -V, --version              Display version information and exit.       For example, given the files              foo1, ..., foo9, foo10, ..., foo278, the commands              rename foo foo0 foo?              rename foo foo0 foo??       will turn them into foo001, ..., foo009, foo010, ..., foo278.       And              rename .htm .html *.htm       will fix the extension of your html files.SEE ALSO       mmv(1), mv(1)AVAILABILITY       The rename command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.                                1 January 2000                       RENAME(1)

需要注意的是,rename使用时需要有三个参数。而且注意一下rename的通配符:

? 可替代单个字符* 可替代多个字符* [charset] 可替代charset集中的任意单个字符
0 0
原创粉丝点击