Search and replace string within all files ending in wp-config.php only - linux

I am trying to search for the string localhost within all wp-config.php files in the home directory of my server and replacing it with myhostname.com without affecting any other files.
here is the seach command i have so far
find /home/ -type f -exec sed -i 's/localhost/myhostname.com/g' {} ;

You may try the below.
sed -i 's/string/myhostname.com/g' *wp-config.php

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.

Mass Rename inside Files including sub directories

I have many file having "example.com" in them and I want to replace it with "example.co" as i change my site tld, i have rename program installed in my server and have root access too.
Can anybody help me in renaming it.
Sample location of files:
/var/html/downloads/folder1/folder1-1/abc(example.com).mp4
/var/html/downloads/folder2/folder2-1/abc(example.com).mp4
/var/html/downloads/folder2/folder2-2/abc(example.com).mp4
/var/html/downloads/folder3/folder3-1/abc(example.com).mp4
I hope some1 will help me out here :)
PS: I used this cmd but got below error:
cmd: find -name "*example*" | xargs -I '{}' rename s/com/co/ '{}'
Error:
For more details see rename(1).
rename: not enough arguments
Usage:
rename [options] expression replacement file...
You don't want to rename but to substitute files in place so the right tool is sed :
find -name '*example*' | xargs -I '{}' sed -i 's/\.com$/.co/' '{}'

Mass Replace String in same directly and sub folder

I am trying to use the following way to replace my string
grep -rl matchstring . | xargs sed -i 's/string1/string2/g'
What I want to achieve is actually currently I got a hardcode domain name like
http://account.mysmallwebsite.com
https://account.mysmallwebsite.com
Because this is quite inflexible, I would prefer just
<?php echo $domainName;?>
the variable of domainName will be set in a php file and all the files will include this file so in future if I change domain, it will be easier to just change 1 place rather than multiple pages.
and replace all occurrence of the website
http://account.mysmallwebsite.com
https://account.mysmallwebsite.com
with the echo string, but how do I achieve it through command line at putty shell ssh
You can use the following:
find . -name "*.php" -exec sed -r -i.bak 's#http(s?)://account.mysmallwebsite.com#<?php echo $domainName;?>#g' {} \;
Explanation
find . -name "*.php" -exec ... {} \; this looks for files whose name ends with .php and performs the command indicated in ....
sed -r -i.bak 's#http(s?)://account.mysmallwebsite.com#<?php echo $domainName;?>#g' file:
this looks for http(s?)://account.mysmallwebsite.com, that is, http + maybe an s + ://account.mysmallwebsite.com and replaces it with <?php echo $domainName;?>.
Note the usage of # as separator instead of the typical /, so that we don't have to escape the slashes in the URL.
The -i.bak creates a backup file with .bak extension, while the original one gets in-place edited.
You can also "play" with -maxdepth value, to define how many levels of subdirectories you want to work on. For example, -maxdepth 1 will just check the current directory, while -maxdepth 2 will also include the sub folders of the given directory.
Graphically:
s#http(s?)://account.mysmallwebsite.com#<?php echo $domainName;?>#g
^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ substitution ^
| | look for this text replace all matches
| may be an "s"
^
search
See the sed command in action:
$ cat a
hello
http://account.mysmallwebsite.com
https://account.mysmallwebsite.com
httpss://account.mysmallwebsite.com
bye
$ sed -r 's#http(s?)://account.mysmallwebsite.com#<?php echo $domainName;?>#g' a
hello
<?php echo $domainName;?>
<?php echo $domainName;?>
httpss://account.mysmallwebsite.com
bye

How to change all occurrences of a word in all files in a directory

I was in the process of creating a User class where one of the methods was get_privileges();.
After hours of slamming my head into the keyboard, I finally discovered that the previous coder who I inherited this particular database spelled the word "privileges" as "privelages" in the MySQL database, and thus also everywhere in the hundreds of files that access these "privelages" it is spelled that way.
Is there a way in Linux (Ubuntu Server) that I can go through every place in the /var/www folder and replace "privelages" with "privileges", so that I don't have to deal with this typo and code around it?
A variation that takes into account subdirectories (untested):
find /var/www -type f -exec sed -i 's/privelages/privileges/g' {} \;
This will find all files (not directories, specified by -type f) under /var/www, and perform a sed command to replace "privelages" with "privileges" on each file it finds.
Check this out: http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/
cd /var/www
sed -i 's/privelages/privileges/g' *
I generally use this short script, which will rename a string in all files and all directory names and filenames. To use it, you can copy the text below into a file called replace_string, run sudo chmod u+x replace_string and then move it into your sudo mv replace_string /usr/local/bin folder to be able to execute it in any directory.
NOTE: this only works on linux (tested on ubuntu), and fails on MacOS. Also be careful with this because it can mess up things like git files. I haven't tested it on binaries either.
#!/usr/bin/env bash
# This will replace all instances of a string in folder names, filenames,
# and within files. Sometimes you have to run it twice, if directory names change.
# Example usage:
# replace_string apple banana
echo $1
echo $2
find ./ -type f -exec sed -i -e "s/$1/$2/g" {} \; # rename within files
find ./ -type d -exec rename "s/$1/$2/g" {} \; # rename directories
find ./ -type f -exec rename "s/$1/$2/g" {} \; # rename files

Resources