【问题标题】:i want to change the names of the files and this change is in the middle. i basically need to remove a part of the variable and write something else我想更改文件的名称,这个更改在中间。我基本上需要删除变量的一部分并写其他东西
【发布时间】:2023-03-05 23:08:01
【问题描述】:

我试过了

$ls 
casts.c endian.c ptr.c signed-unsigned-representations.c signed-unsigned.c test-hard-link.c

$for i in *.c;do mv "$i" "$i"__swa.c; done

$ls
casts.c__swa.c endian.c__swa.c ptr.c__swa.c signed-unsigned-representations.c__swa.c signed-unsigned.c__swa.c test-hard-link.c__swa.c

而且我知道因为我的 i 变量是 *.c 所以当我尝试重命名并添加 (__swa.c) 部分时,它只会添加到变量名上。

我需要像这样重命名文件:

casts__swa.c  endian__swa.c  ptr__swa.c  signed-unsigned-representations__swa.c                signed-unsigned__swa.c  test-hard-link__swa.c

【问题讨论】:

    标签: bash loops file rename parameter-expansion


    【解决方案1】:

    使用 Perl 的独立 renameprename 命令:

    rename -n 's/\./__swa./' *.c
    

    如果输出看起来没问题,删除-n

    【讨论】:

      【解决方案2】:

      使用 Bash 的 parameter expansion ,您可以执行以下操作:

      for f in *.c; do
          echo mv "$f" "${f%%.c}"__swa.c
      done
      

      (当然删除echo,如果它看起来会做你想做的事)

      但我通常更愿意使用 Perl 更灵活的rename,正如 Cyrus 的回答中所建议的那样。

      【讨论】:

        猜你喜欢
        • 2017-11-25
        • 1970-01-01
        • 2020-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-03
        • 2016-07-22
        相关资源
        最近更新 更多