Linux Bash Replace Strings in PHP Files
Page 1 of 1
Linux Bash Replace Strings in PHP Files
- 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
Re: Linux Bash Replace Strings in PHP Files
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!
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\");"
Re: Linux Bash Replace Strings in PHP Files
- Code:
Basically chars like " $ and / need escaping!!!
Last edited by jamied_uk on 23rd February 2020, 16:30; edited 1 time in total
Re: Linux Bash Replace Strings in PHP Files
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 "\/\/ \/\/" "\/\/"
Similar topics
» Replace a string with sed on linux using bash
» FTP Files From Linux With Bash
» Mass Rename Files In Linux Using Bash
» Converting Doc & Docx To PDF Files On Linux With Bash
» Sending & Receiving Binary Files Via Bash On Linux
» FTP Files From Linux With Bash
» Mass Rename Files In Linux Using Bash
» Converting Doc & Docx To PDF Files On Linux With Bash
» Sending & Receiving Binary Files Via Bash On Linux
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum