bash: How to change file extensions
Posted On June 10, 2020
This post shows how to change file extensions using bash.
To change file extensions, we simple need to loop through file in a directory and use the “mv” command to rename each file.
The following code requests directory address from the user and then renames all “.log” files to “.txt“
#!/bin/bash
echo "Specify a directory: "
read folder
cd $folder
for i in *.log
do
mv "$i" `basename $i log`txt
done
Line 2 ask print the message “Specify a directory”.
Line 3 reads response from the user and stored it in the variable “folder”.
Line 4 take the control to that directory
Line 4-8 is the for loop that loop through all files with the extension “.log”.
Line 6 renames .log file to .txt