如何为一堆文件重命名扩展名?

557
在一个目录中,我有一堆*.html文件。我想把它们全部重命名为*.txt
我该怎么做呢? 我使用Bash shell。
28个回答

2
如果您喜欢使用PERL,这里有一个简短的PERL脚本(最初由PERL的创始人Larry Wall编写),可以完全满足您的要求: tips.webdesign10.com/files/rename.pl.txt
对于您的示例,以下内容应该可以解决问题:
rename.pl 's/html/txt/' *.html

2
这个问题很久以前已经得到了回答和接受,而且你的回答似乎没有带来比已经说过的更多的东西。 - ChristopheLec
1
+1,因为这是Larry Wall脚本(由Robin Barker修改)。最后可用的网址是:http://tips.webdesign10.com/files/rename.pl.txt - loretoparisi

1

很遗憾,这不是一件容易的事情,需要一些表达式魔法才能实现可移植性。

for file in *.html; do echo mv -- "$file" "$(expr "$file" : '\(.*\)\.html').txt"; done

如果你已经满意它所做的事情,就可以去掉回声。

编辑:对于这种特殊情况,basename 可能更易读,尽管 expr 在一般情况下更灵活。


虽然这可能不是最好的答案,但对我来说是!我需要一种方法来重命名整个路径中的字符串,而不仅仅是文件名。感谢您的发布! - donut

1
你也可以在Bash中创建一个函数,将其添加到.bashrc或其他地方,然后在任何需要的地方使用它。
change-ext() {
    for file in *.$1; do mv "$file" "$(basename "$file" .$1).$2"; done
}

使用方法:

change-ext css scss

函数中的代码来源: https://dev59.com/gHM_5IYBdhLWcg3wymc0#1224786


如果你收到“Bad substitution”错误,那么“sh”命令可能没有指向bash。将“sh”替换为“/bin/bash”,它应该可以工作。 - Wesley De Keirsmaeker
@WesleyDeKeirsmaeker 这里没有任何与Bash特定的内容,也不会产生“bad substitution”错误(尽管."$1"两处都应该加上引号)。 - undefined

1
最初的回答:

简单易懂!

find . -iname *.html  -exec mv {} "$(basename {} .html).text"  \;

在shell运行之前,"$(basename {} .html).text"会被shell扩展为{}.text,这是错误的。 - undefined

1

以下是我用来将 .edge 文件重命名为 .blade.php 的方法

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

工作得像魔力一样。

0

我在我的.bashrc文件中编写了这段代码

alias find-ext='read -p "Path (dot for current): " p_path; read -p "Ext (unpunctured): " p_ext1; find $p_path -type f -name "*."$p_ext1'
alias rename-ext='read -p "Path (dot for current): " p_path; read -p "Ext (unpunctured): " p_ext1; read -p "Change by ext. (unpunctured): " p_ext2; echo -en "\nFound files:\n"; find $p_path -type f -name "*.$p_ext1"; find $p_path -type f -name "*.$p_ext1" -exec sh -c '\''mv "$1" "${1%.'\''$p_ext1'\''}.'\''$p_ext2'\''" '\'' _ {} \;; echo -en "\nChanged Files:\n"; find $p_path -type f -name "*.$p_ext2";'

在像“/home//example-files”这样的文件夹中,具有以下结构:
  • /home/<user>/example-files:
    • file1.txt
    • file2.txt
    • file3.pdf
    • file4.csv

这些命令的行为将会如下所示:

~$ find-text
Path (dot for current): example-files/
Ext (unpunctured): txt

example-files/file1.txt
example-files/file2.txt


~$ rename-text
Path (dot for current): ./example-files
Ext (unpunctured): txt
Change by ext. (unpunctured): mp3

Found files:
./example-files/file1.txt
./example-files/file1.txt

Changed Files:
./example-files/file1.mp3
./example-files/file1.mp3
~$

“Punctured” 的意思是“有一个小的不希望的洞”; 我猜你的意思是没有标点符号? - undefined

0
你可以使用专门用于批量重命名文件的工具,例如renamer
要重命名当前文件夹中的所有文件扩展名:
$ renamer --find ".html" --replace ".txt" --dry-run * 

这里有更多的使用示例在这里


0

这里有一个解决方案,使用AWK。确保文件存在于工作目录中。否则,请cd到html文件所在的目录,然后执行以下命令:

for i in $(ls | grep .html); do j=$(echo $i | grep -oh "^\w*." | awk '{print $1"txt"}'); mv $i $j; done

这对于稳健性和可移植性来说存在多个问题;建议通过https://shellcheck.net/进行全面诊断。简而言之,[不要在脚本中使用`ls`](https://mywiki.wooledge.org/ParsingLs),并且[引用您的变量](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable)。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接