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

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


当前回答

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

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

很有魅力。

其他回答

试试这个

rename .html .txt *.html 

用法:

rename [find] [replace_with] [criteria]

你也可以在Bash中创建一个函数,将它添加到。bashrc或其他文件中,然后在任何你想使用的地方使用它。

change-ext() {
    for file in *.$1; do mv "$file" "$(basename "$file" .$1).$2"; done
}

用法:

change-ext css scss

源代码的功能:https://stackoverflow.com/a/1224786/6732111

重命名当前目录和子目录下所有文件的扩展名,不需要任何其他包(仅使用shell脚本):

在当前目录下创建shell脚本rename.sh,代码如下: #!/bin/bash 在$(查找。- name“* 1美元”);做 Mv "$file" "${file%$1}$2" 完成 通过./rename.sh .old .new运行它。 例如:./rename.sh .html .txt

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

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

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

如果使用bash,则不需要使用sed、basename、rename、expr等外部命令。

for file in *.html
do
  mv "$file" "${file%.html}.txt"
done