Script to Replace/substitute Multi Occurrences of A String in Files

Last Updated on: August 30, 2020

There are a couple of ways you can replace a string in a file, and an example is using the mv command, but what if you want to replace multiple strings at once, then you might consider creating a loop, which loops through all the file in your target directory, and you then replace the string based on the condition you set.

That’s one way, but the better and simpler way is using the rename command, and I wrote a script for just that:

					

#!/bin/bash
# Author: The_Devsrealm_Guy
# Website: https://devsrealm.com
# Script to replace/substitute multi occurrences of a string in files
# Last Edited: August 2020

read -p "Which directory do you want to execute the script? " dir_name

if  [ -d $dir_name ] ; then
real_dir=$dir_name
read -p "Old string you want to replace? " string_old
read -p "New string you want to replace it with? " new_string

# Perform the rename operation
cd $real_dir && rename ''s/$string_old/$new_string/'' *

#Go back into the script directory
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
cd $SCRIPT_DIR

    echo
    echo renaming completed
    echo
    ls $dir_name

else

    echo "Incorrect directory, please point to an existing directory"
fi

return

Copy the script into a file, e.g renametool.sh, add permission bit to the script: chmod +x renametool.sh, and you then run the script by sourcing it using:. renametool.sh

This is my output:

					

user@server:~/bin$ . tester.sh
Which directory do you want to execute the script? /home/user/bin/backup
Old string you want to replace? .txt
New string you want to replace it with? .exe

renaming completed

book001.exe  book011.exe  book021.exe  book031.exe  book041.exe  book051.exe  book061.exe  book071.exe  book081.exe  book091.exe
book002.exe  book012.exe  book022.exe  book032.exe  book042.exe  book052.exe  book062.exe  book072.exe  book082.exe  book092.exe
book003.exe  book013.exe  book023.exe  book033.exe  book043.exe  book053.exe  book063.exe  book073.exe  book083.exe  book093.exe
book004.exe  book014.exe  book024.exe  book034.exe  book044.exe  book054.exe  book064.exe  book074.exe  book084.exe  book094.exe
book005.exe  book015.exe  book025.exe  book035.exe  book045.exe  book055.exe  book065.exe  book075.exe  book085.exe  book095.exe
book006.exe  book016.exe  book026.exe  book036.exe  book046.exe  book056.exe  book066.exe  book076.exe  book086.exe  book096.exe
book007.exe  book017.exe  book027.exe  book037.exe  book047.exe  book057.exe  book067.exe  book077.exe  book087.exe  book097.exe
book008.exe  book018.exe  book028.exe  book038.exe  book048.exe  book058.exe  book068.exe  book078.exe  book088.exe  book098.exe
book009.exe  book019.exe  book029.exe  book039.exe  book049.exe  book059.exe  book069.exe  book079.exe  book089.exe  book099.exe
book010.exe  book020.exe  book030.exe  book040.exe  book050.exe  book060.exe  book070.exe  book080.exe  book090.exe  book100.exe

Looks handy, I guess 😉

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

Your email address will not be published. Required fields are marked *