ssh question - how do I search my server for a bit of text?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wolfy
    Confirmed User
    • Dec 2003
    • 3574

    #1

    ssh question - how do I search my server for a bit of text?

    I need to search all the files on my entire domain for a bit of text.


    I'm still sending traffic to an old sponsor (that doesn't pay) and I can't figure out where it's coming from

    I know there's an ssh command I can run - anybody know how to do it?
  • Fizzgig
    Registered User
    • Feb 2004
    • 9649

    #2
    I don't know but we keep our sites mirrored on a HD for just such an emergency.
    ---'-,-{@ Sassy Grrrl @}-'-,---

    Comment

    • boldy
      Macdaddy coder
      • Feb 2002
      • 2806

      #3
      cd <to the target directory>
      grep -Ri "yourpiece of text here" *

      MacDaddy Coder.

      Comment

      • Wolfy
        Confirmed User
        • Dec 2003
        • 3574

        #4
        is there any way to recursively search all directories, say public_html and below?

        oh, and say I want to search for foobar - do I use the quotes or no?

        grep -Ri "foobar" *

        Comment

        • boldy
          Macdaddy coder
          • Feb 2002
          • 2806

          #5
          Originally posted by Wolfy
          is there any way to recursively search all directories, say public_html and below?

          oh, and say I want to search for foobar - do I use the quotes or no?

          grep -Ri "foobar" *
          you first : change the directory to public_html

          say : cd /web/sites/yourdomain.com/public_html

          then

          grep -Ri foobar *

          if its a single word you can drop the quotes



          *edit* the capital R is recursive .. the lowercase i is case insensitive ..
          MacDaddy Coder.

          Comment

          • Wolfy
            Confirmed User
            • Dec 2003
            • 3574

            #6
            so that will search public_html, public_html/directory1, public_html/dirctory1/subdirectory, and all the files in each?

            I might be stretching here, but is it possible to do a find and replace with ssh? :D

            Thanks for the help

            Comment

            • Wolfy
              Confirmed User
              • Dec 2003
              • 3574

              #7
              oh, and will it find all instances of the text? Like if foobar is surronded with html, or other text - will it find 123foobar123?

              Comment

              • bl4h
                Confirmed User
                • Jul 2006
                • 1282

                #8
                btw its not an ssh command. its a shell command. thats like calling "http://www.gfy.com" an IE command

                remember that ;/

                Comment

                • woj
                  <&(©¿©)&>
                  • Jul 2002
                  • 47880

                  #9
                  Originally posted by bl4h
                  btw its not an ssh command. its a shell command. thats like calling "http://www.gfy.com" an IE command

                  remember that ;/
                  come on man, ssh=secure shell, close enough...
                  Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
                  Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
                  Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager

                  Comment

                  • boldy
                    Macdaddy coder
                    • Feb 2002
                    • 2806

                    #10
                    Originally posted by Wolfy
                    oh, and will it find all instances of the text? Like if foobar is surronded with html, or other text - will it find 123foobar123?
                    yes it will , search and replace is kind of tricky tho to do with a shell command ..
                    MacDaddy Coder.

                    Comment

                    • Wolfy
                      Confirmed User
                      • Dec 2003
                      • 3574

                      #11
                      Originally posted by bl4h
                      btw its not an ssh command. its a shell command. thats like calling "http://www.gfy.com" an IE command

                      remember that ;/
                      well then.. learn something new every day :D

                      Comment

                      • Wolfy
                        Confirmed User
                        • Dec 2003
                        • 3574

                        #12
                        ouch - that gave me a ton of results, all mixed up with html!

                        Can I narrow it down? Say, print a list of files that contain foobar and their locations?

                        Comment

                        • boldy
                          Macdaddy coder
                          • Feb 2002
                          • 2806

                          #13
                          I'm in a good mood today so i wrote this little perl script for you :
                          uploaded it and call it test.pl

                          then run it from the command shell:
                          (make sure you're into the public_html dir)


                          perl test.pl


                          Code:
                          #!/usr/bin/perl
                          # by boldy / EarlmillerCash.com
                          # Now promote Earlmillercash.com goddamnnit :) 
                          
                          my $lSearch = "foobar";
                          my $lReplace = "whatever";
                          
                          # this will search and replace the above words, and it will create
                          # filename.html.test files if it matches the action.
                          # if you think it workds set $lTest to "" instead of ".test"
                          #
                          # and it will overwrite the file.
                          # make sure you're in the right directory by cd ing itto it
                          # eg .
                          # cd /web/sites/yourdomain.com/public_html
                          
                          # set $lTest to "" when you think this works
                          # to delete the *.test files run: find ./ -name *\.test -exec rm {} \;
                          
                          my $lTest = ".test";
                          my @lFiles = split ("\n" , `find ./ -type f` );
                          
                          foreach my $lFile (@lFiles) {
                             print "Opening file " . $lFile . "\n";
                             open (INFILE,"<" . $lFile);
                             my @lLines =<INFILE>;
                             close INFILE;
                          
                             my $lWrite = 0;
                             foreach my $lLine (@lLines) {
                                if ($lLine =~ m/$lSearch/) {
                                      $lWrite = 1; last;
                                }
                             }
                             if ($lWrite) {
                                open (OUTFILE , ">" . $lFile . $lTest );
                                foreach my $lLine (@lLines) {
                                   $lLine =~ s/$lSearch/$lReplace/g;
                                   print OUTFILE $lLine;
                                }
                                close OUTFILE;
                                print $lFile.$lTest  . " created \n";
                             }
                          }
                          MacDaddy Coder.

                          Comment

                          • Wolfy
                            Confirmed User
                            • Dec 2003
                            • 3574

                            #14
                            will that work if I use any text for the replacement?

                            Here's the thing - I was promoting one sponsor for a long time, and they ended up burning me. To avoid this trouble in the future, I'm now using this instead of an actual hardlink:

                            <?php include('full/path/to/sponsor.php'); ?>

                            where sponsor.php is simply a file with one line in it - my current affiliiate link.

                            So I am effectively replacing this:

                            <a href="http:juicybucks.com/owesme3k/andwontpay.php">link text</a>

                            with this:

                            <a href="<?php include('full/path/to/sponsor.php'); ?>">link text</a>

                            I'm assuming it will work fine, but just to be sure and to get an extra dig into the deadbeat fucks that inspired this thread

                            Comment

                            • boldy
                              Macdaddy coder
                              • Feb 2002
                              • 2806

                              #15
                              Code:
                              my $lSearch = quotemeta ("<a href=\"http//:juicybucks.com/owesme3k/andwontpay.php\">link text</a>");
                              my $lReplace = "<a href=\"<?php include('full/path/to/sponsor.php'); ?>\">link text</a>";
                              Thank should work

                              replace the bogus info ofcoz ..

                              make sure you escape (\) the quotes within your texts like a href=\"http://
                              MacDaddy Coder.

                              Comment

                              • borked
                                Totally Borked
                                • Feb 2005
                                • 6284

                                #16
                                find / -name 'whatyouwanttosearchfor' -print

                                replace / with whatever directory you want to search.

                                ---edit
                                oops sorry, this will only find filenames - didn't read that you wanted to search files for text.

                                ehm, transfer your server files to a mac and use spotlight?
                                Last edited by borked; 09-16-2006, 01:57 PM.

                                For coding work - hit me up on andy // borkedcoder // com
                                (consider figuring out the email as test #1)



                                All models are wrong, but some are useful. George E.P. Box. p202

                                Comment

                                • borked
                                  Totally Borked
                                  • Feb 2005
                                  • 6284

                                  #17
                                  Originally posted by boldy
                                  I'm in a good mood today so i wrote this little perl script for you :
                                  uploaded it and call it test.pl

                                  then run it from the command shell:
                                  (make sure you're into the public_html dir)


                                  perl test.pl


                                  Code:
                                  #!/usr/bin/perl
                                  # by boldy / EarlmillerCash.com
                                  # Now promote Earlmillercash.com goddamnnit :) 
                                  
                                  my $lSearch = "foobar";
                                  my $lReplace = "whatever";
                                  
                                  # this will search and replace the above words, and it will create
                                  # filename.html.test files if it matches the action.
                                  # if you think it workds set $lTest to "" instead of ".test"
                                  #
                                  # and it will overwrite the file.
                                  # make sure you're in the right directory by cd ing itto it
                                  # eg .
                                  # cd /web/sites/yourdomain.com/public_html
                                  
                                  # set $lTest to "" when you think this works
                                  # to delete the *.test files run: find ./ -name *\.test -exec rm {} \;
                                  
                                  my $lTest = ".test";
                                  my @lFiles = split ("\n" , `find ./ -type f` );
                                  
                                  foreach my $lFile (@lFiles) {
                                     print "Opening file " . $lFile . "\n";
                                     open (INFILE,"<" . $lFile);
                                     my @lLines =<INFILE>;
                                     close INFILE;
                                  
                                     my $lWrite = 0;
                                     foreach my $lLine (@lLines) {
                                        if ($lLine =~ m/$lSearch/) {
                                              $lWrite = 1; last;
                                        }
                                     }
                                     if ($lWrite) {
                                        open (OUTFILE , ">" . $lFile . $lTest );
                                        foreach my $lLine (@lLines) {
                                           $lLine =~ s/$lSearch/$lReplace/g;
                                           print OUTFILE $lLine;
                                        }
                                        close OUTFILE;
                                        print $lFile.$lTest  . " created \n";
                                     }
                                  }

                                  For coding work - hit me up on andy // borkedcoder // com
                                  (consider figuring out the email as test #1)



                                  All models are wrong, but some are useful. George E.P. Box. p202

                                  Comment

                                  • Wolfy
                                    Confirmed User
                                    • Dec 2003
                                    • 3574

                                    #18
                                    Originally posted by boldy

                                    make sure you escape (\) the quotes within your texts like a href=\"http://

                                    That's the bit I was looking for. Thanks man - and I'll look forward to collecting a few checks from you if you have something I can use. The join page to your program looks nice, I'll put some effort into it.


                                    Cheers man!

                                    Comment

                                    • Wolfy
                                      Confirmed User
                                      • Dec 2003
                                      • 3574

                                      #19
                                      Originally posted by boldy
                                      I'm in a good mood today so i wrote this little perl script for you :
                                      uploaded it and call it test.pl

                                      then run it from the command shell:
                                      (make sure you're into the public_html dir)


                                      perl test.pl

                                      Hey boldy, I gave it a shot, and it didn't seem to work. It listed every file on my server, but nothing was changed. I followed up a second time with a test - I created your test.pl, with the replacement text as follows:

                                      Code:
                                      my $lReplace = "testtesttest";
                                      Then I ran the command: grep -Ri testtesttest * and the only file I found was the test.pl file.

                                      Any ideas?

                                      Comment

                                      • the alchemist
                                        Confirmed User
                                        • Dec 2004
                                        • 3271

                                        #20
                                        Track it?
                                        264 349 400

                                        Comment

                                        • fatdicksimon
                                          Confirmed User
                                          • Feb 2006
                                          • 226

                                          #21
                                          you can use sed to do a search and replace as well if that perl script isn't working. syntax is as follows:

                                          sed 's|oldtext|newtext|g' -i filename

                                          that will search filename for oldtext and replace it with newtext. make sure to escape any single quotes within the containing single quotes as well.

                                          for multiple files, as far as i know, you must specify a file extension, but you can use the * symbol to search and replace a bunch.

                                          so you will probably want to navigate to the containing directory, then do as follows:

                                          sed 's|oldtext|newtext|g' -i *.php

                                          of course you can do

                                          sed 's|oldtext|newtext|g' -i /otherdirectory/*.php

                                          if you want to find and replace a file in another directory.

                                          when you first mess around with it, you may only want to check one file and make a backup of it to check the results.

                                          sed runs quickly and is very useful. do a 'man sed' command for more info.

                                          hope that helps!
                                          icq: 199791893
                                          holler at me

                                          Comment

                                          • Wolfy
                                            Confirmed User
                                            • Dec 2003
                                            • 3574

                                            #22
                                            is there a command similar to "grep -iR text *" that will find the files and then create a text file so I know which files they are, and their location? Or perhaps a command that will find the files containing that text but then only list the filenames, rather than the file + the code/text I searched for?

                                            Comment

                                            Working...