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

Roo*_*oot 37 bash powershell cmd

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

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

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

Smi*_*Smi 84

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

ren *.txt *.c
Run Code Online (Sandbox Code Playgroud)

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

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

对于Linux(Bash):

for file in *.txt
do
 mv "$file" "${file%.txt}.c"
done
Run Code Online (Sandbox Code Playgroud)

  • 用于递归搜索的“Dir -Recurse” (6认同)