1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

SSH Query - Replace Text "Multiple Files & Directories"

Discussion in 'Linux and Unix' started by tgo316, Jan 25, 2005.

Thread Status:
Not open for further replies.
Advertisement
  1. tgo316

    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
     
  2. Squashman

    Squashman 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.
     
  3. tgo316

    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
     
  4. Squashman

    Squashman 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.
     
  5. tgo316

    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
     
  6. Squashman

    Squashman 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.
     
  7. tgo316

    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
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Do all the files end in .html
     
  9. tgo316

    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
     
  10. Squashman

    Squashman 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 "*.*"`
     
  11. tgo316

    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
     
  12. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Correct.
     
  13. tgo316

    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
     
  14. Squashman

    Squashman 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.
     
  15. Squashman

    Squashman 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.
     
  16. Sponsor

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 733,556 other people just like you!

Loading...
Similar Threads - Query Replace Text
  1. buster775
    Replies:
    8
    Views:
    446
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/323226

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice