bash 如何重命名一堆文件的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I rename the extension for a bunch of files?
提问by bmw0128
In a directory, I have a bunch of *.html
files. I'd like to rename them all to *.txt
在一个目录中,我有一堆*.html
文件。我想将它们全部重命名为*.txt
How can I do that? I use the bash shell.
我怎样才能做到这一点?我使用 bash shell。
采纳答案by Mikael Auno
For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.
要获得更好的解决方案(只有 bash 功能,而不是外部调用),请参阅其他答案之一。
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):
以下内容可以并且不需要系统拥有该rename
程序(尽管您通常会在系统上拥有该程序):
for file in *.html; do
mv "$file" "$(basename "$file" .html).txt"
done
EDIT: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.
编辑:正如评论中所指出的,这不适用于没有正确引用(现在在上面添加)中带有空格的文件名。当纯粹在您自己的文件上工作时,您知道文件名中没有空格,这将起作用,但是每当您编写可能在以后重复使用的内容时,请不要跳过正确的引用。
回答by ghostdog74
If using bash, there's no need for external commands like sed, basename, rename, expr, etc.
如果使用 bash,则不需要外部命令,如 sed、basename、rename、expr 等。
for file in *.html
do
mv "$file" "${file%.html}.txt"
done
回答by Amber
rename 's/\.html$/\.txt/' *.html
does exactly what you want.
做你想要的。
回答by Steven Lizarazo
This worked for me on OSX from .txt to .txt_bak
这在 OSX 上对我有用,从 .txt 到 .txt_bak
find . -name '*.txt' -exec sh -c 'mv "rename -S .html .txt *.html
" "${0%.txt}.txt_bak"' {} \;
回答by DaveR
You want to use rename
:
你想使用rename
:
rename 's/\.html$/\.txt/' *.html
This does exactly what you want - it will change the extension from .html
to .txt
for all files matching *.html
.
这正是您想要的 - 它会将所有匹配的文件的扩展名从.html
更改.txt
为*.html
。
Note: Greg Hewgill correctly points out this is not a bash builtin; and is a seperate Linux command. If you just need something on Linux this should work fine; if you need something more cross-platform then take a look at one of the other answers.
注意:Greg Hewgill 正确地指出这不是 bash 内置;是一个单独的 Linux 命令。如果你只需要在 Linux 上的东西,这应该可以正常工作;如果您需要更多跨平台的东西,请查看其他答案之一。
回答by izilotti
回答by Bheru Lal Lohar
For Ubuntu Users :
对于 Ubuntu 用户:
rename -n 's/\.htm$/\.html/' *.htm
回答by bdombro
This is the slickest solution I've found that works on OSX and Linux, and it works nicely with git too!
这是我发现的最巧妙的解决方案,适用于 OSX 和 Linux,而且它也适用于 git!
find . -name "*.js" -exec bash -c 'mv "$1" "${1%.js}".tsx' - '{}' \;
find . -name "*.js" -exec bash -c 'mv "$1" "${1%.js}".tsx' - '{}' \;
and with git:
并使用 git:
find . -name "*.js" -exec bash -c 'git mv "$1" "${1%.js}".tsx' - '{}' \;
find . -name "*.js" -exec bash -c 'git mv "$1" "${1%.js}".tsx' - '{}' \;
回答by A.A
Here is an example of the rename command:
以下是重命名命令的示例:
rename -v 's/\.htm$/\.html/' *.htm
The -n means that it's a test run and will not actually change any files. It will show you a list of files that would be renamed if you removed the -n. In the case above, it will convert all files in the current directory from a file extension of .htm to .html.
-n 表示这是一次测试运行,实际上不会更改任何文件。如果删除 -n,它将向您显示将重命名的文件列表。在上述情况下,它会将当前目录中的所有文件从 .htm 的文件扩展名转换为 .html。
If the output of the above test run looked ok then you could run the final version:
如果上述测试运行的输出看起来没问题,那么您可以运行最终版本:
$ rename -v 's/\.htm$/\.html/' *.htm
3.htm renamed as 3.html
4.htm renamed as 4.html
5.htm renamed as 5.html
The -v is optional, but it's a good idea to include it because it is the only record you will have of changes that were made by the rename command as shown in the sample output below:
-v 是可选的,但包含它是一个好主意,因为它是您将拥有的由 rename 命令所做的更改的唯一记录,如下面的示例输出所示:
rename -v 's/\.htm$/\.html/' *.htm
The tricky part in the middle is a Perl substitution with regular expressions, highlighted below:
中间棘手的部分是使用正则表达式的 Perl 替换,突出显示如下:
zmv '(*).*' '.txt'
回答by Michael Leonard
This question explicitly mentions Bash, but if you happen to have ZSH available it is pretty simple:
这个问题明确提到了 Bash,但如果你碰巧有 ZSH 可用,它非常简单:
autoload -U zmv
If you get zsh: command not found: zmv
then simply run:
如果你得到zsh: command not found: zmv
那么只需运行:
And then try again.
然后再试一次。
Thanks to this original articlefor the tip about zmv.
感谢这篇原创文章提供了关于 zmv 的提示。