如何重命名一堆文件的扩展名? [英] How do I rename the extension for a bunch of files?

查看:74
本文介绍了如何重命名一堆文件的扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在目录中,我有一堆*.html文件.我想将它们全部重命名为*.txt

In a directory, I have a bunch of *.html files. I'd like to rename them all to *.txt

我该怎么做?我使用bash外壳.

How can I do that? I use the bash shell.

推荐答案

有关更好的解决方案(仅具有bash功能,而不是外部调用),请参见

For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.

以下将执行并且不需要系统具有rename程序(尽管您通常在系统上具有此程序):

The following would do and does not require the system to have the rename program (although you would most often have this on a system):

for file in *.html; do
    mv "$file" "$(basename "$file" .html).txt"
done

如注释中所指出,这对于没有适当引号(现在已在上面添加)的文件名不起作用.当纯粹在自己的文件上工作(您知道文件名中没有空格)时,这将起作用,但是只要您编写以后可以重用的文件,就不要跳过正确的引号.

As pointed out in the comments, this does not work for filenames with spaces in them without proper quoting (now added above). When working purely on your own files that you know do not have spaces in the filenames this will work but whenever you write something that may be reused at a later time, do not skip proper quoting.

这篇关于如何重命名一堆文件的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆