【问题标题】:How to replace file's names with numbers starting with certain number?如何用以某个数字开头的数字替换文件名?
【发布时间】:2018-05-20 22:05:38
【问题描述】:

我希望文件以 177.jpg、178.jpg 等命名,以 177.jpg 开头。 我用它把它们从 1 重命名为文件数量:

ls | cat -n | while read n f; do mv "$f" "$n.jpg"; done 

如何修改?但全新的脚本也很棒。

【问题讨论】:

    标签: linux bash shell filenames


    【解决方案1】:

    Bash 可以为您做简单的数学运算:

    mv "$f" $(( n + 176 )).jpg
    

    希望没有文件名包含换行符。

    有比解析ls 的输出更安全的方法,例如迭代扩展的通配符:

    n=177
    for f in * ; do
        mv "$f" $(( n++ )).jpg
    done
    

    【讨论】:

      【解决方案2】:

      这应该可行。

      #!/bin/bash
      c=177;
      for i in `ls | grep -v '^[0-9]' | grep .png`; # This will make sure only png files are selected to replace and only the files which have filenames which starts with non-numeric
       do
           mv "$i" "$c".png;
          (( c=c+1 )); 
      done
      

      【讨论】:

      • ./1script.sh:第 4 行:意外标记附近的语法错误 mv'<br> ./1script.sh: line 4: mv $i $c.jpg;'
      • 看起来您在进行复制粘贴时出现了换行符,因此出现了错误。
      • 你的意思是
        在评论 - 不,它只是在这里:我试图在评论中插入
      • 它非常适合我不知道你的版本有什么问题
      • 我一定是搞砸了,现在没事了,谢谢。
      猜你喜欢
      • 2019-07-02
      • 2021-11-22
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多