facebook youtube pinterest twitter reddit whatsapp instagram

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

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 ;)

Related Post(s)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Understanding The Four (4) Ways Command-Line Runs Shell Script

    Sometimes we need to think like the command line shell in order to understand how things are done in the background, this would make debugging a breeze. In this guide, you would learn all the variou

  • Bash Script For Optimizing ClassicPress & WordPress Images in Bulk

    Out of frustration of the way different plugin optimizes images (which is super limited, and not effective; it has never been for me)in the WordPress world, I decided to create a script that does it

  • Suppressing Sensitive Entered Text/String In Bash

    So far in our examples or scripts, we have written in the previous guides, we have no way of controlling what is visible to user, which is fine if we have no sensitive data such as passwords. As soon

  • read prompts in bash script

    We've previously taken a quick look at the builtin read command, where we use the built-in read to populate the $REPLY variable, which holds the value of read when a variable is not supplied to read

  • Using read in a Bash Script

    We previously looked at using echo with options in a shell, in this guide we would combine it with read command to prompt for user input. Create and open up a new text file: nano $HOME/bin/hw3.sh Ad