【问题标题】:how to delete filename's specific suffix in a dir, in linux [duplicate]如何在linux中删除目录中文件名的特定后缀[重复]
【发布时间】:2012-08-10 07:16:30
【问题描述】:

可能重复:
Extract filename and extension in bash
Linux: remove file extensions for multiple files

For example, A.txt B.txt, I want to rename then to A and B . 

我如何使用 shell 脚本来做到这一点?还是其他方法?谢谢。

【问题讨论】:

    标签: linux shell rename


    【解决方案1】:

    我会使用类似的东西:

    #!/bin/bash
    
    for file in *.txt
    do
        echo "$file" "$( echo $file | sed -e 's/\.txt//' )"
    done
    

    当然,将“.txt”的上述两个引用替换为您要删除的任何文件扩展名,或者最好只使用 $1(第一个传递给脚本的参数)。

    迈克尔 G.

    【讨论】:

    • $(ls *.txt) 应该只是*.txt$file 应该被引用为"$file"(以及"$( echo ... )"),正则表达式应该是's/\.txt$//'(@987654328 @flag 的意思是“做多个替换”,这不是你想要的,你只想要一个)。
    • 谢谢,我会改代码的。
    【解决方案2】:
    for i in *.txt; do mv "$i" "${i%.txt}"; done
    

    【讨论】:

      【解决方案3】:
      for FILE in *.txt ; do mv -i "$FILE" "$(basename "$FILE" .txt)" ; done
      

      【讨论】:

        猜你喜欢
        • 2023-03-20
        • 1970-01-01
        • 2023-01-23
        • 1970-01-01
        • 2014-04-15
        • 2019-07-03
        • 2014-03-19
        • 1970-01-01
        • 2020-04-19
        相关资源
        最近更新 更多