How To Batch Rename TXT files to JSON

Introduction

I needed to rename a bunch of .txt files to .json and I was too lazy to hand type it.
I found a quick bash script which can be ran in Mac OSX Terminal or any shell.

Renaming the files (dry run)

This script will print out the new file names but won't rename them. This is to ensure, the bash script is renaming correct .txt files.

1) cd into the directory
2) Then type in the command touch rename.sh. This will create a new shell script.
3) We need to set permissions first by making it executable. chmod 755 rename.sh.
4) Copy and paste this to rename.sh

for file in *.txt
do
  echo mv "$file" "$(basename "$file" .txt).txt"
done
rename.sh

5) Run ./rename.sh

Dry run of renaming .txt to .json files

Renaming .txt to .json

If your dry run is successful, then we can remove echo from script. echo is a print function of bash, similar to console.log for Javascript.

for file in *.txt
do
  mv "$file" "$(basename "$file" .txt).txt"
done
rename.sh

Conclusion

You can run this script for any file extension as long as you change the .txt and json.