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

时间:2022-08-25 23:58:21

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

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

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

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

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

1 个解决方案

#1


74  

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

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

ren *.txt *.c

In PowerShell, it is better to use the Path.ChangeExtension method instead of -replace (thanks to Ohad Schneider for 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

#1


74  

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

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

ren *.txt *.c

In PowerShell, it is better to use the Path.ChangeExtension method instead of -replace (thanks to Ohad Schneider for 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