批量重命名文件名

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

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

1.使用mv命令

话不多少,直接上代码

#!/bin/bash

for file in `ls`
do
    newfile=`echo $file | sed 's/\.txt/\.csv/'`
    mv $file $newfile
done

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

2.使用rename命令

rename .txt .csv *

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

RENAME(1)                  Linux Programmer’s Manual                 RENAME(1)

NAME
       rename - Rename files

SYNOPSIS
       rename from to file...
       rename -V

DESCRIPTION
       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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值