SSH Find and Replace

tgo316

Verified User
Joined
May 4, 2005
Messages
61
Hello,
First things first am not a linux guy and have no experience into SSH codes and all...

So if i've been given the code and some directions, then yes i can perform it easily as i have done that in the past before.

My concern is,
I have .html files on my server, i have almost 50000 of them basically.

The path of a gif file has gone wrong unfortunately and I wish to replace the path within that piece of html code.

So basically i need to replace html text within .html files.
I know there's a way through SSH that i could perform this function.

A code example will help

Please Help!
Best Regards
Amit
 
if you have mysql installed (which you more than likely do if you running DA, then you have an easy to use utility called 'replace'.

To find out how to use it type 'man replace' in a terminal (SSH) window.
 
mike_p

yes i do have mysql installed...i tried looking out for ur option, but couldnt understand a thing. Since am not into linux and am more comfortable with asp, access and windows.

i am actually concerned of how to even get text strings inside .html files replaced through the use of mysql alone? mysql is a database which wont have any connection with .html

sorry if i have sounded stupid...but am very new to linux and stuff

A piece of code of what to do through an ssh login (i use putty.exe) would help me solve the issue easily.

Best Regards
Amit
 
'Replace' is a utility program that is installed with mysql. Apart from that it has nothing to do with mysql. It is a good program for replacing text in files.

To use it to (for example) to replace '<HTML>' with '<html>'
in all html files in the current directory you could enter the following command

PHP:
replace '<HTML>' '<html>' -- *.html

There are other programs you could use, but, in my opinion, this is one of the simplest.
 
thanks but...

mike_p
thanks for the php solution, i shall try that out soon :) really i just forgot that i could use php code for it too.

hostpc.com
i used this code
perl -pi -e 's/cartshop.gif/http:\/\/shoejamboree.com\/cartshop.gif/g' *.html

it works in the root directories, but doesn't enter the sub directories. I have around 450 plus sub-directories.
Any code modifications that would help it work into the sub directories as well?

Best Regards
Amit
 
Well, I make NO guarantees that this would work, but here's a code snipped I had in my toolbox:

#!/usr/bin/perl



#################################################
# Perl script to replace words in HTML/PHP files.
# It recurses (goes down) all directories.
# Author: Aaron Bazar
# Free software licensed under the GPL.
# http://www.zzzgifts.com/script.html
# Last updated: 09/27/2004
# USE AT YOUR OWN RISK. BACK UP YOUR FILES
#################################################

############################################ PUT THE DIRECTORY YOU WANT TO START THE ADD REPLACE FUNCTION

##### NO TRAILING SLASH
#######################################

$startDirectory = "/full/path/to/start/directory";

###################################################### THIS IS THE EXTENSION OF THE FILES YOU WISH TO EDIT.

##### AS WRITTEN, IT WILL EDIT ALL .html FILES
#################################################
$extension=".html";

###################################################### THIS IS THE TEXT STRING TO FIND - THE ONE YOU
##### WANT CHANGED TO SOMETHING ELSE. Change to fit your needs.

#################################################

$find = "© 2003";

#################################################
##### THIS IS THE TEXT THAT SHOULD BE USED AS A REPLACEMENT
##### Change to fit your needs.
#################################################

$replace = "© 2003-2004.";

##########MAIN PROGRAM BEGINS HERE DO NOT EDIT ########
#######################################################

$extension= "\\".$extension;
use File::Find;
find(\&runMain, "$startDirectory");
sub runMain {

if (/$extension/ ) {
$file= $_;
next unless (-r $file);

open(PAGE,"$File::Find::name") || die "I can't open $file";

while(<PAGE>){

$thisPage= $thisPage . $_;

}#while

close(PAGE);

$thisPage =~ s#$find#$replace#g;

open(PAGENEW,">$File::Find::name") || die "I can't write the new html to $file";

print PAGENEW $thisPage;

close(PAGENEW);

$thisPage="";

}#if

}#runMain
 
Last edited:
nopes didnt work unfortunately. got all kinds of errors

:(

Best Regards
Amit
 
LOL :p
I hit the submit button and I was thinking to myself, boy that domain sounds familiar, oh well.
:p


tgo316,
What are the errors your recieve when trying to use that code?
 
now what do i say?

:( pls dont poke fun at me when i say this....i surely am not a linux guy :P

Sorry the code u gave me hostpc.com i thought i was supposed to use it through ssh, it gave me chunk loads of errors there.

I also do own my own server, now .php works fine, but when i created a .pl or a .cgi file, it gives me a message

403 Forbidden
You don't have permission to access /1.pl on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


--------------------------------------------------------------------------------

Apache/1.3.33 Server at shoejamboree.com Port 80

So i really don't know how to run this script without .cgi or .pl configured on my server.
I uploaded these files in my cgi-bin directory, but i still got the same message.

Don't know what should i do :(

Geez! I need to get a hang on linux :(

Best Regards
Amit
 
You need to make the nessary changes to the file. Like the full path to the parent directory of which you want the script to start its search and replace. Be very careful since it's recursive.

Then you need to configure what your looking for and what you want it replaced with. Check the file extension.
Then log in as root through ssh and create a new file named something like "sar.pl"
pasting your modified script in it.
Save and close the file.
Do a whereis or which perl and make sure that the path to perl matches the shebang line of your perl script (the very first line beginning with #!)

Then chmod 755 sar.pl
and finally execute it
./sar.pl

You can't execute it through the web using apache because of permission problems.

If the script still fails to execute you might need to specify the path to perl before executing it. That information can be obtained using the which or whereis.
For example if perl is really located at /usr/local/bin/perl
your would execute the script using something like

/usr/local/bin/perl ./sar.pl
 
Last edited:
jmstacey

Hi jmstacey :) thanks for the help again, am sure this problem will be solved once you tell me the command of how to create a file through ssh. and the command for saving it :D
I have logged in as root

:D ;)
Gosh! i don't know if u've ever answered to anyone more weak on linux, ssh then me on these forums.

Best Regards
Amit
 
Text editors vary from flavor to flavor, but vi is usually always available.

man vi will give you the manual for some cozy midnight reading.

But to give you a general idea you would have vi create a new file.
vi sar.pl
Then you would either paste or retype the script like in the old text only editors back in the day.

And :wq Saves and closes the file.
 
saved the file and chmod it to 755

now yes i can't really execute it. I read what u wrote about executing it in ur earlier post.

I checked the path and i am 99% sure it is
/usr/local/bin/perl

so while i am logged into root, i just typed this /usr/local/bin/perl ./sar.pl
and said enter. it gave me a message saying no such file or directory

Help!
 
Back
Top