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

时间:2012-08-25 08:13:11

标签: bash powershell cmd

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

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

  • Windows中的Powershell
  • Windows中
  • cmd / DOS
  • bash中的终端

1 个答案:

答案 0 :(得分:82)

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

ren *.txt *.c

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

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

对于Linux(Bash):

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