How to change a bunch of file extensions in linux?

28 Views Asked by At

I was trying to change a bunch of files extensions from yml to yaml, and I've found a very good answer here in Stackoverflow. Based on that answer which was:

for file in *.yml
do
  mv "$file" "${file%.yml}.yaml"
done

I've turned it into a command to make my life easier, which I've tried to share there as a comment, but since it required me 50 points to add a comment, I couldn't, then I decided to create this question as a kind of post. How can we convert that answer into a command:

Create the file chgext to add the script

vi chgext

Copy and paste the script below into the chgext file
#!/bin/bash

for file in *.$1
do
  mv "$file" "${file%.$1}.$2"
done

Save and exit pressing: esc + : + x + enter

Make chgext executable

chmod +x chgext

Move it to /usr/local/bin so it can be called from any directory as any other linux command

sudo mv chgext /usr/local/bin

Now you are ready to go, call chgext passing old_extension and new_extension as params, like this from the directory where your files are

chgext yml yaml

The original answer is here

0

There are 0 best solutions below