bash 如何更改目录中多个文件的扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12120326/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:35:33  来源:igfitidea点击:

How do I change the extension of many files in a directory?

bashpowershellcmd

提问by Root

Suppose I have a large number of files in a directory with .txtextension.

假设我在一个带有.txt扩展名的目录中有大量文件。

How can I change the extension of all these files to .cusing the following command line environments:

如何将所有这些文件的扩展名更改为.c使用以下命令行环境:

  • Powershell in Windows
  • cmd/DOS in Windows
  • The terminal in bash
  • Windows 中的 Powershell
  • Windows 中的 cmd/DOS
  • bash中的终端

回答by Smi

On Windows, go to the desired directory, and type:

在 Windows 上,转到所需目录,然后键入:

ren *.txt *.c

In PowerShell, it is better to use the Path.ChangeExtensionmethod instead of -replace(thanks to Ohad Schneiderfor the remark):

在 PowerShell 中,最好使用该Path.ChangeExtension方法而不是-replace(感谢Ohad Schneider的评论):

Dir *.txt | rename-item -newname { [io.path]::ChangeExtension($_.name, "c") }

For Linux (Bash):

对于 Linux (Bash):

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