GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   ssh question - how do I search my server for a bit of text? (https://gfy.com/showthread.php?t=656171)

Wolfy 09-16-2006 01:26 PM

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 :1orglaugh

I know there's an ssh command I can run - anybody know how to do it?

Fizzgig 09-16-2006 01:27 PM

I don't know but we keep our sites mirrored on a HD for just such an emergency.

boldy 09-16-2006 01:30 PM

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

:thumbsup

Wolfy 09-16-2006 01:32 PM

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" *

boldy 09-16-2006 01:35 PM

Quote:

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 .. :thumbsup

Wolfy 09-16-2006 01:43 PM

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 :)

Wolfy 09-16-2006 01:44 PM

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

bl4h 09-16-2006 01:47 PM

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

remember that ;/

woj 09-16-2006 02:11 PM

Quote:

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

boldy 09-16-2006 02:15 PM

Quote:

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

Wolfy 09-16-2006 02:17 PM

Quote:

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

Wolfy 09-16-2006 02:21 PM

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?

boldy 09-16-2006 02:34 PM

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";
  }
}


Wolfy 09-16-2006 02:44 PM

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 :winkwink:

boldy 09-16-2006 02:53 PM

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://

borked 09-16-2006 02:55 PM

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?

borked 09-16-2006 03:00 PM

Quote:

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";
  }
}


:thumbsup :thumbsup

Wolfy 09-16-2006 03:11 PM

Quote:

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!

Wolfy 09-16-2006 05:19 PM

Quote:

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?

the alchemist 09-16-2006 05:27 PM

Track it?

fatdicksimon 09-16-2006 05:37 PM

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!

Wolfy 09-16-2006 06:23 PM

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?


All times are GMT -7. The time now is 02:19 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123