SSH Query - Replace Text "Multiple Files & Directories"

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
Hello!
This is my first post out here! Please Help!!! :)

I have been trying to replace a certain text term on all files of my server (A couple of thousand files)

The problem is, I don't really have the backup of these files locally and I need to perform a find and replace funtion on my server.

I have access to ssh and use "putty" as a tool for the same. I am a Windows guy and have very little exposure to the universe of linux and opensource.

I can handle "putty" on a certain set of commands that i remember.
Example: tar zxvf filename.tar.gz (Unzips a file on the server)

So, if I could know the command for replacing a certain text term in one/all my directories and files. It would help a ton.
Example:
Text term to find: abc
Text term to replace: gef

Thanks in advance for all your kind help!

Best Regards
Amit
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
You have a few options to do this. One would be to write a script to find all files in your directory, pipe it to the cat command and then pipe that output to an inline text editor like SED.

There is also a one line Perl command that you could use as well. Do a google search for Perl One Liners. I had to replace the path to perl on about 40 cgi's I had in my cgi-bin when I moved to a new web host. I found a single perl command to replace the path with the one I needed.
 

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
I am not a perl/php guy.
I am a windows guy. but my server is a unix server.

I am more into .asp

So I use a tool "putty" to run commands like unzipping tar.gz files on the server.

So If there's some command through ssh, it can help me.

I can work a bit with php, i also found a code. But the server time out problem is something i couldnt solve.

Please Help

Best Regards
Amit
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Putty & ssh has nothing to do with this other then giving you access to your server. It is basically the same as sitting at a DOS prompt on your windows machines. Tell me the exact text you need to replace in the files and I will work on a shell script for you.
 

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
LwdSquashman
Yes i know that putty is basically just like a command prompt, it's just the interface.

Well the text to find: 2454762304831160
text to replace: 4913967272763932

There are multiple files and they are in multiple directories under one root folder.
.htm file type

Thanks A Ton, do let me know how to run the shell script too - Sorry am a novice, but with the right instructions, i will be able to handle things right.

Best Regards
Amit
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Now you have to understand that Unix Shells do not like spaces in directory and filenames. So if you have spaces in your directories or filenames, let me know now or change the spaces with underscores. It is alot easier for me to code it, if you dont have any spaces in your directory or filenames.
 

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
my filenames and directory names have no spaces in them.

I always use something like hello-abc.hrml or hello-abc(foldername)

Best Regards
Amit
 

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
90% of them in .html
10% of them in .php

So i dont mind running the script twice, if the script can only be altered to one file type.


Best Regards
Amit
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
This command will parse thru EVERY file in your directories and change the text for you.

perl -pi -e s/2454762304831160/4913967272763932/g `find . -name "*.*"`

If you want it to just change html files you would just change the find statement.

perl -pi -e s/2454762304831160/4913967272763932/g `find . -name "*.html"`

If you want it to make a backup of the file before it makes changes to the file you can do this.

perl -pi.bak -e s/2454762304831160/4913967272763932/g `find . -name "*.*"`
 

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
LwdSquashman
Hi,
The above command is supposed to be executed in the ssh command prompt right?

Best Regards
Amit
 

tgo316

Thread Starter
Joined
Jan 25, 2005
Messages
9
I did the same,
first i entered my username and password
then entered the destination directory. Then typed the following code
perl -pi -e s/2454762304831160/4913967272763932/g `find . -name "*.html"`

------------------------------------
But it gave me an error
Cant open find. -name "*.*"
perl -pi -e s/2454762304831160/4913967272763932/g `find: No such file or directory.
Can't do inplace edit: No such file or directory.
Can't open -name: No such file or directory
Can't open *.html: No such file or directory

Please Help!

Best Regards
Amit
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
The only thing I can think of is that they dont allow you to use the find command. I have had that happen a couple of times on a few shell accounts I had in the past. But just to make sure, don't type it in. Copy and paste what I have above. You might have mistyped something. To paste something into putty just copy it, then switch over to your putty session and right click in it.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
One other thing. Does your user have rights to these files? You will need read & write.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top