在一个目录中,我有一些*.html文件。我想把它们都重命名为*.txt

我该怎么做呢?我使用bash shell。


当前回答

这个问题明确提到了Bash,但如果你恰好有可用的ZSH,它是相当简单的:

zmv '(*).*' '$1.txt'

如果你得到zsh:命令没有找到:zmv,然后简单地运行:

autoload -U zmv

然后再试一次。

感谢这篇关于zmv的原创文章。

其他回答

在Mac上……

如果你没有安装rename: brew Install rename 重命名-S .html .txt *.html

在Linux或window git bash或window的wsl中,尝试以下命令,只需一行代码就可以更改当前目录或子目录甚至其子目录中的每个文件的扩展名

find . -depth -name "*.html" -exec sh -c 'mv "$1" "${1%.html}.txt"' _ {} \;

如果你更喜欢PERL,这里有一个简短的PERL脚本(最初由PERL的创造者Larry Wall编写),它将完全满足你的需求: tips.webdesign10.com/files/rename.pl.txt。

对于你的例子,下面的方法应该可以奏效:

rename.pl 's/html/txt/' *.html

这是我用来重命名。edge文件为。blade.php

for file in *.edge; do     mv "$file" "$(basename "$file" .edge).blade.php"; done

很有魅力。

rename 's/\.html$/\.txt/' *.html

这正是你想要的。