62

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
ruffin
  • 16,507
  • 9
  • 88
  • 138
Root
  • 789
  • 1
  • 5
  • 5
  • 6
    Not sure why this was closed (question seems pretty clear to me) but in any case essentially the same question can be found (open) here: http://stackoverflow.com/questions/13382638/how-can-i-bulk-rename-files-in-powershell/13382966 – Ohad Schneider Mar 26 '16 at 22:51
  • 2
    @OhadSchneider Sort of... unfortunately the title (though not the actually use case) of that one is "How can I bulk rename files in PowerShell?", which is much broader, as are the answers. Smi's answer here is money for extensions. Still, no idea why this is marked as too broad either. ¯\\_(ツ)_/¯ – ruffin Jan 31 '18 at 15:39

1 Answers1

137

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
Smi
  • 13,850
  • 9
  • 56
  • 64