Method 1: Bash loop
The most common way to change file extensions recursively in a directory is to use a bash for loop. We can prompt the user to enter the target directory, old extension, and the new extension to rename using a bash script.
Step 1: Assemble the script
Let us begin assembling the script. The first part we need is to get the target directory. For this, we can bash read as:
echo "Enter the target directory "
read target_dir
cd $target_dir
echo "Enter the file extension to search without a dot"
read old_ext
echo "Enter the new file extension to rename to without a dot"
read new_ext
echo "$target_dir, $old_ext, $new_ext"
The script above will ask the user for the directory to process and then cd into the set directory.
Next, we get the old extension without the dot (.); finally, we get the new extension to rename the files.
Now let us get to processing the files. For this; we can implement a simple rule that recursively searches the files as:
do
mv -v "$file" "${file%.$old_ext}.$new_ext"
done;
The for loop above will search the passed directory for all files with the old extension and rename them to the new extension.
To get verbose, we use the mv command with -v. You can suppress this output by replacing the -v flag with –
Step 2: Run the script
Now, let us put the script to the test. The final script is below:
echo "Enter the target directory "
read target_dir
cd $target_dir
echo "Enter the file extension to search without a dot"
read old_ext
echo "Enter the new file extension to rename to without a dot"
read new_ext
echo "$target_dir, $old_ext, $new_ext"
for file in *.$old_ext
do
mv -v "$file" "${file%.$old_ext}.$new_ext"
done;
In this test, we will use the /var/log directory and rename all the .log files to .bak. Here are the contents of the directory before running the script.
Now, let us run the script.
$ sudo ./extensions
The screenshot above shows the script processing the files and renaming all the files with .log to .bak.
Since this is an interactive script, it comes in handy when you do not want to hard code the extension.
The following is the contents of the /var/log directory after the script.
To revert the changes, switch the old extension to .bak and the new extension as .log
Method 2: Rename command
If you do not feel like working with a script, you can use the rename tool to change the file extensions recursively.
To install rename, use the command:
Once installed, you can use the rename command as:
cd /var/log/
# change extension
sudo rename 's/\.log/.bak/' *.log
To revert the changes, change the .bak to .log and vice versa.
Method 3: MMV command
You can also use the mmv command that allows you to move multiple files simultaneously. Install mmv with the command:
To rename files with mmv command:
mmv "*.csv" "#1.xls"
The #1 moves the files to the current directory. Once you run the command, it will rename all .log files to the specified extension.
Summing Up
This article discussed various methods you can recursively rename file extensions in a specific directory. However, it is good to note that you can implement strategies other than those discussed in this guide.
Thank you for reading, and remember to share!