amazon media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

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:

# Author: The_Devsrealm_Guy
# Website:
# 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
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]}")

    echo renaming completed
    ls $dir_name


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


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

This is my output:

user@server:~/bin$ .
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)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (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

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f

  • Sort in Bash Scripting

    We recently just looked at grep in bash scripting, and another commonly used program in bash scripting is the program called sort. Sort is a simple filter that is used to sort lines of text from its