find and replace .htaccess code - linux

I want to find and remove code in all .htaccess files and directories under /home directory
RedirectMatch \.(dynamiccontent|pl|plx|perl|cgi|php|php4|php4|php6|php3|shtml)$ http://domain.com/cgi-sys/movingpage.cgi
What is the bash command to do this job ?
edit:
i tried this command
find /home*/*/public_html/ -mindepth 1 -iname "\.htaccess" -type f -exec grep -Hi "RedirectMatch*" '{}' \;
but this command find only the code , not remove it , also the code find all redirectmatch code not the specific code i mention at the first of my question

You can probably try this sed command:
sed -i.bak '/RedirectMatch \\\.(dynamiccontent/d' .htaccess

Related

Search and replace URL in all files

I'm looking to run script or command to mass change a string that is a URL. I've viewed many examples on this forum, however none are working.
I have created a .sh file to run the following:
$SRC='$url = "https://www.myurl.com/subdir/process.do"';
$DST='$url="https://api.myurl.com/subdir/process.do"';
find . -type f -name "*.php" -exec sed -i 's/$SRC/$DST/g' {} +;
This is not working. I'm thinking it may because of having backslashes in the search content? The search/replace is needed to be run across all sub-directories on .php files.
Any assistance would be greatly appreciated.
Thanks!
First thing - check your variable definitions. In bash, variable definitions usually do not start with a leading $. Ie, should be:
SRC='$url = "https://www.myurl.com/subdir/process.do"';
DST='$url="https://api.myurl.com/subdir/process.do"';
Next, you should switch to using single quotes for the pattern, and double quotes for the variable, as per:
https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command
Example that seems to work:
sed -i 's,'"$SRC"','"$DST"','
UPDATE: This exact script works perfectly for me on Linux:
#!/bin/bash
SRC='$url = "https://www.myurl.com/subdir/process.do"';
DST='$url="https://api.myurl.com/subdir/process.do"';
find . -type f -name "*.php" -exec sed -i 's,'"$SRC"','"$DST"',' {} \;
Contents of file "asdf.php" created in home directory (before running script):
$url = "https://www.myurl.com/subdir/process.do"
Contents of file "asdf.php" after running script:
$url="https://api.myurl.com/subdir/process.do"

Mass Find/Replace within files having specific filename under command line

I am looking for a quick command to search all .htaccess files for a specific IP address and change it to another IP address from the command line
something like
grep -rl '255.255.254.254' ./ | xargs sed -i 's/254/253/g'
I know the above example is a bad way to do it, just an example (and showing I did some searching to find a solution
Search: files with filename .htaccess (within 2 levels deep of current path?)
Find: 255.255.254.254
Replace with: 255.255.253.253
or, is this too much to ask of my server and I would be better off replacing them as I find them?
Try:
find . -type f -name '.htaccess' -execdir sed -i 's/255\.255\.254\.254/255.255.253.253/g' {} +
How it works:
find .
Start looking for files in the current directory.
-type f
Look only for regular files.
-name '.htaccess'
Look only for files named .htaccess.
-execdir sed -i 's/255\.255\.254\.254/255.255.253.253/g' {} +
For any such files found, run this sed command on them.
Because . is a wildcard and you likely want to match only literal periods, we escape them: \.
We use -execdir rather than the older -exec because it is more secure against race conditions.

Linux replace a word in all files

I am using Linux CentOS. I have many folders inside my www directory and there are a lot of files inside those folders. I would like to change in those files:
www.mysite.com
to
www.myNewSite.com
Is there a way to run q command and that will replace all?
You can use sed command. Below is the command. I tested and seems working.
[chatar#/Users/chatar]$ find test -name '*.php'
test/folder1/one.php
test/folder1/two.php
test/folder2/four.php
test/folder2/three.php
[chatar#/Users/chatar]$ find test -type f -name '*.php' -exec grep www {} \;
www.mysite.com
www.mysite.com
www.mysite.com
www.mysite.com
[chatar#/Users/chatar]$ find test -type f -name '*.php' -exec sed -i -e 's/mysite/myNewSite/g' {} \;
[chatar#/Users/chatar]$ find test -type f -name '*.php' -exec grep www {} \;
www.myNewSite.com
www.myNewSite.com
www.myNewSite.com
www.myNewSite.com
[chatar#/Users/chatar]$
sed --in-place 's/www.mysite.com/www.myNewSite.com/g' *.php
should do the trick.
You can also use a Perl on-liner:
perl -pi -e 's/www\.mysite\.com/www.myNewSite.com/g' *.php
If you want to keep copies of the original files (with .bak extensions), use:
perl -pi.bak -e 's/www\.mysite\.com/www.myNewSite.com/g' *.php

How to ignore directories and certain patterns when supplying command-line arguments

I'm a newbie at Linux, and I am wondering if there's a 'one-liner' command that allows me to link everything in a directory to another directory, but ignoring subdirectories and certain wildcards from the source directory.
Let's be more specific...let's say I want to link everything in /foo to /bar/tmp as in...
ln -s /foo/* /bar/tmp/.
...but I want to:
ignore any subdirectories in /foo
ignore any files with the wildcard
runscript*
Any suggestions on how to do this?
You could use find like this
find /foo -maxdepth 1 -type f ! -name 'runscript*' -exec ln -s {} /bar/tmp/ \;
Something like
cd /bar/tmp
find /foo -maxdepth 1 -a -type f -a \! -name 'runscript*' |
while read file; do
ln -s "$file"
done
could do the trick.

Find/Replace through all files on the root directory through SSH

I just noticed that my website got hacked and all the .php files have a base64 encode string on line 1. The string is the same across every file so...
How can I search my entire root directory and remove this?
try:
find /path/to/dir -type f -name '*.php' -exec sed -i '/string-to-remove/{1d;}' '{}' \;
edit:
added -name '*.php' to restrict this to .php files.
added sed matching directive

Resources