943,829 Members | Top Members by Rank

Ad:
Jul 8th, 2009
0

Listing files with certain text

Expand Post »
Pretty new at shell scripting. My server was hacked and instances of links for viagra were placed in it. I have lots of sites, so I need to search for all instances of "viagra" and list the files, so I can go remove the intrusions. Can this be done using shell?

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daprezjer is offline Offline
1 posts
since Jul 2009
Jul 9th, 2009
0

Re: Listing files with certain text

I'm going out on a limb and assuming you are using linux. If that is the case you can use grep to find the word:
bash Syntax (Toggle Plain Text)
  1. sk@svn:/tmp$ ls
  2. sk@svn:/tmp$ mkdir -p ./some/junk/dir
  3. sk@svn:/tmp$ echo "viagra" >> ./some/junk/dir/somefile.txt
  4. sk@svn:/tmp$ grep -l -r -i viagra .
  5. ./some/junk/dir/somefile.txt

For grep --
-l: list files, not the lines
-r: recursive
-i: case insensitive
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 10th, 2009
0

Re: Listing files with certain text

Are you using wordpress it happened with me few days back ... the reason was the old version of wordpress
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parul1234 is offline Offline
2 posts
since Jul 2009
Jul 10th, 2009
0

Re: Listing files with certain text

Click to Expand / Collapse  Quote originally posted by daprezjer ...
Pretty new at shell scripting. My server was hacked and instances of links for viagra were placed in it. I have lots of sites, so I need to search for all instances of "viagra" and list the files, so I can go remove the intrusions. Can this be done using shell?

Thanks.
Yes, it can. A simplistic way to find all affected files, leaving you to do the rest of the work:
Shell Scripting Syntax (Toggle Plain Text)
  1. find / -type f -exec grep -li viagra {} \;

Or, assuming you are on a reasonably POSIX-ish system, a more complex way to find and edit all affected files, minimizing the effort you must exert:
Shell Scripting Syntax (Toggle Plain Text)
  1. find / -type f -exec grep -li viagra {} \; | while read a; do
  2. echo -n "Press <ENTER> to edit "
  3. tput smso
  4. echo -n "$a"
  5. tput rmso
  6. echo " or <CTRL/C> to quit"
  7. read b </dev/tty
  8. vi "$a" </dev/tty
  9. done

The tput() commands assist clarity, but aren't vital; delete them if your system doesn't speak terminfo. This is re-entrant. You don't have to remember where you were when you took a break or erred; once the noxious content has been removed, find() won't find it again.

This works on Debian Linux.
Last edited by Fest3er; Jul 10th, 2009 at 2:05 pm.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: add shortcuts to all users desktop
Next Thread in Shell Scripting Forum Timeline: Problem with equal mark in input variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC