PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Linux Bash Replace Strings in PHP Files

Go down

Linux Bash Replace Strings in PHP Files Empty Linux Bash Replace Strings in PHP Files

Post by jamied_uk 9th February 2020, 11:08



Code:
#!/bin/bash
# (c) J~Net 2022
# https://jnet.forumotion.com/t1681-linux-bash-replace-strings-in-php-files#2599
#
# Usage
#      ./replace.sh string new-string
#
echo "Welcome To Find & Replace String (c) J~Net 2022 jnet.sytes.net"
echo ""
# 1st find files with this
# grep " = " /var/www/html/*.php
#
# sudo find /var/www/html -type f -name "*.php" -exec sed -i 's/http:\/\//https:\/\//g' "{}" \;
# To find string inside all files in a folder
# grep "redeem reward" /home/tom/*.txt
# grep " = " /var/www/html/*.php
# grep "jnet.forumn.biz" /var/www/html/*.php
#
#
# grep "include '" /var/www/html/*.php
#
# include 'tracker.php';
#
# do a test to see the outcome before executing on a load of files or you may break things!
#sudo find /var/www/html -type f -name "*.php" -exec sed -i "s/ = /=/g" {} \;
#
if [ -z "$1" ]
then
      echo "Enter string to find"
      read var
      echo ""

else
        var=$1

fi
#
if [ -z "$2" ]
then
      echo "Enter replace string"
      read rep
else
        rep=$2

fi
#
sudo find /var/www/html -type f -name "*.php" -exec sed -i "s/$var/$rep/g" {} \;
echo "Mission Complete!"
echo ""
#

Above script requires sudo!


Last edited by jamied_uk on 17th June 2022, 03:04; edited 3 times in total
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Linux Bash Replace Strings in PHP Files Empty Re: Linux Bash Replace Strings in PHP Files

Post by jamied_uk 23rd February 2020, 14:33

Another example!
Say a site closes down and your site used to rely on this site! and you have thousands of pages to edit and comment out lines refereing to a dead site this is easy with following example!

Code:
./web_file_replace.sh "require_once('tracker.php');" "\/\/include(\"tracker.php\");"
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Linux Bash Replace Strings in PHP Files Empty Re: Linux Bash Replace Strings in PHP Files

Post by jamied_uk 23rd February 2020, 14:37

Code:
Basically chars like " $ and / need escaping!!!
 (maybe more)


Last edited by jamied_uk on 23rd February 2020, 16:30; edited 1 time in total
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Linux Bash Replace Strings in PHP Files Empty Re: Linux Bash Replace Strings in PHP Files

Post by jamied_uk 23rd February 2020, 16:09

Even More Examples

Code:
./web_file_replace.sh "require_once('tracker.php');" "\/\/include(\"tracker.php\");"
./web_file_replace.sh "include (\"tracker.php\");" "\/\/include(\"tracker.php\");"
./web_file_replace.sh "require('tracker.php');" "\/\/include(\"tracker.php\");"

./web_file_replace.sh "<? " "<?php "

./web_file_replace.sh "\/\/\/\/" "\/\/"

 ./web_file_replace.sh "\/\/ \/\/" "\/\/"
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Linux Bash Replace Strings in PHP Files Empty Re: Linux Bash Replace Strings in PHP Files

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum