【问题标题】:Changing files from .cfg to .txt [duplicate]将文件从 .cfg 更改为 .txt [重复]
【发布时间】:2021-05-05 23:46:40
【问题描述】:

我可以将单个文件的 .cfg 更改为 .txt >

[root@cal]# mv abc.cfg abc.txt

[root@cal]# ls | grep abc.txt

abc.txt

【问题讨论】:

    标签: linux bash file


    【解决方案1】:

    这应该适用于大多数 shell。

    如果您想在不调用 sed 的情况下进行操作,那么 bash 中有模式替换,但是......那么我将不得不在谷歌上搜索该语法。

    for x in *.cfg;do mv $x `echo $x | sed -e 's/cfg/txt/g'`;done
    

    【讨论】:

      【解决方案2】:

      您可以使用 for 循环 对匹配模式的文件列表进行操作:

      for file in *.cfg
      do
         mv $file "${file%cfg}txt"
      done
      

      或者,在一行中:

      for file in *.cfg; do mv $file "${file%cfg}txt"; done
      

      百分号运算符在用作 shell 变量的一部分时,会从字符串末尾删除 cfg。 bash howto 是关于此(和其他操作)的非常有用的参考:https://tldp.org/LDP/abs/html/string-manipulation.html

      【讨论】:

        猜你喜欢
        • 2019-03-20
        • 2015-05-25
        • 2016-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        • 2012-12-06
        相关资源
        最近更新 更多