[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

Find & Replace All Files URGENT

Hello,

I wanted to know how I am going to find an replace the following files

.pl
.html
.cgi
.txt

Let say I wanted to find $your in all the above files and then replace that with $this

How I am going to do that using my linux redhat 6.2 server .

Thanks.
0
sohaib69
Asked:
sohaib69
1 Solution
 
ozoCommented:
perl -pi -e 's/\$your/\$this/g' *.pl *.html *.cgi *.txt
0
 
sohaib69Author Commented:
ok sir,

as you have mention

perl -pi -e 's/\$your/\$this/g' *.pl *.html *.cgi *.txt


I wanted to find and replce that inside the following folder

/home/admin/www/

How I will do that.
0
 
SakanaCommented:
cd /home/admin/www/
perl -pi -e 's/\$your/\$this/g' *.pl *.html *.cgi *.txt


He he....
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sohaib69Author Commented:
also one more thing,

I want that command to change all the files cos there are other directories inside /home/admin/www/main/

so how that command will change

perl -pi -e 's/\$your/\$this/g' *.pl *.html

every files going inside other directories too such as

/home/admin/www/ and /home/admin/www/main/

If that cant be done then Do I have to change by going on to every directory.

Thanks
0
 
toschCommented:
#!/bin/bash

ext="html pl"


for e in $ext ; do echo $e; for f in `find -type f -name '*'$e` ; do
    perl -pi -e 's/\$your/\$this/gs' $f
done ; done
0
 
sohaib69Author Commented:
thanks a lot. u are a reall expert also will u help me with changing few things i will pay you for the job you will do for me.

my email is sohaib@sohaib.com email me and let me know.

Thanks.
0
 
sohaib69Author Commented:
Excellent JOB
0
 
sohaib69Author Commented:
I have one more question could you help me in finding the files using find command , I wanted to find the following

Testing

let me know how I am going to find that Testing in all files and directories.

Thanks
0
 
sohaib69Author Commented:
I tried that.

#!/bin/bash

ext="html pl"


for e in $ext ; do echo $e; for f in `find -type f -name '*'$e` ; do
   perl -pi -e 's/\$your/\$this/gs' $f
done ; done


it run fine but didnt change anything .

why is that
0
 
sohaib69Author Commented:
also i am trying to change like

perl -pi -e 's/\THIS/\WITHTHIS/g' *.pl *.html


Its not changing that too.
0
 
toschCommented:
perl -pi -e 's/this/that/g' myfile.txt

this line changes all occurences of the word "this" to "that" in file myfile.txt. matching is case sensitive, this means "this" is not equal to "THIS".
it does not work under cygwin.

ext="html pl"
for e in $ext ; do
   # what stands here will be executed 2 times
   # one time with $e evaluating to html and one time
   # with $e evaluating to pl
done


find -type f -name '*.html' # this lists all regular files with a name ending with "html". probably it won't find all files because of symbolic links (man find for details)

the inner for loop iterates through the entries put out by the find command and executes the code enclosed for each entry
0
 
toschCommented:
grep this * # finds the occurence of the word "this" in all files in the current directory (man grep for details)

well, you should be able to build a simple script to recurse directories
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now