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

BashPowershellCmd

Bash Problem Overview


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

Bash Solutions


Solution 1 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRootView Question on Stackoverflow
Solution 1 - BashSmiView Answer on Stackoverflow