Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

  • Powershell in Windows
  • cmd/DOS in Windows
  • The terminal in bash
like image 733
Root Avatar asked Aug 25 '12 08:08

Root


People also ask

How do I change the extension of multiple files in PowerShell?

Change file extensions with PowerShell You can also use the Rename-Item to change file extensions. If you want to change the extensions of multiple files at once, use the Rename-Item cmdlet with the Get-ChildItem cmdlet.


1 Answers

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

ren *.txt *.c 

In PowerShell, it is better to use the Path.ChangeExtension method instead of -replace (thanks to Ohad Schneider for the remark):

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

For Linux (Bash):

for file in *.txt do  mv "$file" "${file%.txt}.c" done 
like image 133
Smi Avatar answered Sep 20 '22 02:09

Smi