Need help searching for text in files using command line

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I am trying to find out if any files on my website are referencing a particular javascript file. How can I do this over ssh using command line tools? I have tried something like this:

grep -l 'detect_flash.js' *.php

But it just searches files within that directory, not all files recursively. I need to search all files on the server, including inside directories.

Thanks for any help,

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

This should do it:

find /home/user/public_html/  -name "*".php  -type f -print0  | xargs -0 grep "detect_flash.js" | uniq -c  | sort -u  | cut -d":" -f1

Just replace /home/user/pulic_html/ with the path to search. I came across this command when searching for malicious scripts. More information can be found at this article.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

When I do that it says:

find: bad option -print0
find: path-list predicate-list
xargs: illegal option -- 0

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

What system is this on?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Can't you just tack on a -r to grep recursively?

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

That's what I thought but it doesn't work either, says that -r is an illegal option.

pr0gr4mm3r - it's an apache/linux server. Not sure the details on that though. I've had some other weird problems with command line tools on it though so maybe I should bug the sys admins...

Edit: server admin told me this is on Solaris, which doesn't have the same kind of tools as Gnu/linux Confused

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

The command works on my CentOS (Redhat-based) server, and my Ubuntu (Debian-based) desktop. Not sure why it wouldn't work on yours, and the linked article doesn't mention any compatible problems.

He has: 3 posts

Joined: Nov 2008

grep -e 'pattern' -r directory

…always works for me. I use it several times a day.

If -r is really an illegal option for your grep for some reason, try:

grep -e 'pattern' -d recurse

If that still doesn't work, time for good ol' man grep. Its man page is pretty monstrous, but if you search around for "recursively," you ought to find a solution.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.