Automate File Renaming with a Shell Script
Introduction: Renaming files is a common task in any operating system. It can be time-consuming and tedious when you have a large number of files to rename. This is where a shell script can come in handy. In this article, we will create a shell script that will rename all files in a specified directory with a certain pattern or format.
Prerequisites:
Basic knowledge of the Linux terminal
Access to a Linux machine with bash installed
Steps:
Open your terminal and create a new file using your favorite text editor. Let's name it "rename_files.sh".
Add the following code to the file:
#!/bin/bash
# author : ajay patel
# date : 19-april-2023
# version : 1
#path for directory
path="/home/ubuntu/scripts/dir_for_file_name_change"
#Extension to match
pattern=".txt"
#new file name
new_name="new_file_"
#counter for file names
counter=1
#for loop to find specific file extenstion and change name of all
for file in "$path"/*"$pattern"; do
mv "$file" "$path/$new_name$counter.txt"
((counter++))
done
echo "All files have been renamed"
Replace the "/home/ubuntu/scripts/dir_for_file_name_change" in the script with the actual path to the directory containing the files you want to rename.
The script will rename all files in the specified directory with the new name and a number appended to it.
Conclusion: In this article, we created a shell script that automates the task of renaming files in a directory. This script can be easily modified to suit your specific requirements by changing the file extension, the new name, and the starting number. With this script, you can save time and avoid the hassle of manually renaming files.
Happy scripting!
#ShellScripting #BashScripting #FileRenaming #Automation #DevOps #LinuxCommands



