【问题标题】:How can I append an extension to files that don't already have one?如何将扩展名附加到还没有扩展名的文件?
【发布时间】:2013-06-25 21:46:06
【问题描述】:

我有一个 cron 将 .txt 附加到目录中的每个文件。

find /path/to/directory -type f -exec mv '{}' '{}'.txt \;

不幸的是,它继续附加.txt,即使它已经拥有它!

我的目录现在看起来像这样...

file1.txt.txt.txt.txt.txt.txt.txt.txt.txt
file2.txt.txt.txt.txt.txt.txt.txt.txt.txt

你有什么推荐的?

【问题讨论】:

    标签: unix batch-file cron grep find


    【解决方案1】:

    您必须修改find 命令以排除带有扩展名的文件。

    find /path/to/files ! -name "*.*" -type f -exec mv '{}' '{}'.txt \;
    

    但是,这也意味着它会忽略以下文件:

    i.am.a.file.with.no.extension
    

    因此,如果您有上述文件,那么最好使用regex 选项在$ 处索引.

    【讨论】:

    • 谢谢!将我的所有多个 .txt.txt 扩展重命名为正常的任何提示? ;)
    • 您可以执行find . -name "*.txt.txt" -type f -exec rename 's/.txt$//' {} \; 之类的操作,将它们归一化为具有单个.txt 扩展。
    • 谢谢,很遗憾我没有rename,所以我会四处寻找其他选项。
    • 抱歉,如果您使用的是bash,那么您可以使用find -name "*.txt.txt" -type f | while read f; do mv "$f" "${f%.txt}"; done
    猜你喜欢
    • 2019-07-07
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2016-12-24
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多