【问题标题】:Remove multiple extensions from all files in a directory (Bash)从目录中的所有文件中删除多个扩展名 (Bash)
【发布时间】:2012-11-05 07:03:07
【问题描述】:

由于我第一次尝试这样做时搞砸了,我现在有一个目录,其中包含现在命名如下的文件:

valery-special-music-poetry.txt.mtxt.md.md.txt.mtxt.md.md.md

我需要命名:

valery-special-music-poetry.md

如何从命令行执行此操作?

我正在运行:GNU bash,版本 3.2.48(1)-release (x86_64-apple-darwin12)

【问题讨论】:

    标签: bash file-rename


    【解决方案1】:
    for file in *; do mv "$file" "${file/.*./.}"; done
    

    【讨论】:

    【解决方案2】:

    壳牌本身:

    for i in *; do mv "$i" "${i%%.*}.${i##*.}"; done
    

    除非您的目录是单级目录,否则我建议您使用 find,如果您在 Apple 中有的话。

    【讨论】:

      【解决方案3】:

      这可能对你有帮助

      #!/bin/bash
      for filename in *
      do
          x=`echo $filename | sed 's/\..*\./\./g'`
          mv $filename $x
      done
      

      将其保存到名为 rename.sh 的文件中

      chmod +x rename.sh
      ./rename.sh
      

      【讨论】:

        【解决方案4】:
        ls -1 *.txt.mtxt.md.md.txt.mtxt.md.md.md | nawk -F"." '{cmd="mv "$0" "$1".md";system(cmd)}'
        

        【讨论】:

        • 我认为 awk -vFS=".txt" 会更安全
        猜你喜欢
        • 2014-07-11
        • 1970-01-01
        • 2022-12-14
        • 1970-01-01
        • 2023-03-12
        • 2020-07-20
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多