Linux replace a word in all files - linux

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

Related

Linux find folder and rename

I want to rename all .hg_gg folders in /var/www to .hg. How can I do it?
I know how to rename .hg to .hg_gg.
find /var/www -name ".hg" -exec bash -c 'mv $0 $0_gg' {} \;
but don't know how to make reverse change.
Try this:
find /var/www -name ".hg_gg" -execdir bash -c 'mv {} .hg' \;
You need to use a special syntax defined by find: {} is the placeholder for the current file name. Check the man page for that. Also it is important to use -execdir instead of -exec. execdir changes the current working directory to the folder where the found directory is located. Otherwise it would do something like this mv /var/www/.hg_gg ./.hg
You can speed up things a bit when restricting find to find folders only using -type d:
find /var/www -type d -name ".hg_gg" -execdir bash -c 'mv {} .hg' \;
Consider this find command with -execdir and -prune options:
find /var/www/ -type d -name ".hg_gg" -execdir mv '{}' '.gg' \; -prune
-execdir will execute the command in each subdirectory
-prune causes find to not descend into the current file
Not a one liner, but you could do this:
for file in `find /var/www -name ".hg_gg"`; do
mv $file `echo $file | sed 's/hg_gg$/hg/'`
done

excluding a directory from find and sed rename

I'm using this command to go through all files, directories and subdirectories to change any mentions of oldurl.com to newurl.org:
find . -type f -name '*.php' -exec sed -i 's|oldurl.com|newurl.org|g' {} +
It works fine, however, I need to exclude three sub-directories from ANY CHANGES: /cache, /archive and /etc as changing the urls with the above command in these paths breaks other scripts.
Haven't had much luck finding an answer... Is it even possible?
Many thanks for any suggestions/help.
Use finds -not Option:
find . -type f -name '*.php' -not \( -path './etc/*' -o -path './cache/*' -o -path './archive/*' \) -exec sed -i 's|oldurl.com|newurl.org|g' {} \;

find & sed: remove lines

I am trying to delete some line in PHP files. I tried to use an find, exec combination:
find . -name '*.php' -exec sed '/#category/d' {} \;
but it only prints out the files contents. Is there anythin wrong in the syntax? Or what is the problem?
Could you try this command:
find . -name '*.php' -exec sed -i '/#category/d' {} \;
I think you've missed -i option
It works, but probably not how you expect.
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Will kill the lines in question.
This should be the command for sed so try to add -i :
sed -i ".bak" '/culpa/d' test.txt
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Source of the answer:
Bash - find a keyword in a file and delete its line

search multiple php files, and replace text?

i have several php files, and there's references to .html files in those.
I need to replace ".html" to ".php"
how can i do this in bash ?
for file in $(find . -name "*.php"); do
sed "s/\.html/.php/g" $file > $$ && mv $$ $file
done
find -name '*.php' -exec sed -ie 's:.html:.php:g' {} \;
Try sed:
find -name "filenamepattern.php" -print0 | xargs -0 sed 's/\.html/\.php/g'
GNU find
find /path -type f -iname '*.php' -exec sed -i.bak 's/\.html/\.php/g' {} +;

Grep a string recursively in all .htaccess files

How can I grep for a certain string recursively in all .htaccess files?
grep -r -n -H -I 'string' .htaccess
doesn't seem to work.
I'm on a GNU Linux system.
cd to a folder before the folders that store the htaccess
$find . -name ".htaccess" -exec grep -r -n -H -I 'string' {} \;
Use the --include option:
grep -rnHI 'pattern' --include=.htaccess .
You can also specify to 'find' that it needs to look for regular files only:
$ find /usr/local -type f -name ".htaccess" -exec grep -rnHI 'pattern' {} \;
You can specify from where your search should begin. For this example, 'find' will look into all directories and sub directories under /usr/local.

Resources