Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am searching for a class declaration on a site with hundreds of PHP files. How can I do this in the current folder and subfolders using grep?

I tested cding to the folder and then something like

grep -r 'class MyClass' *.php
share|improve this question

migrated from Jan 19 '10 at 19:13

This question came from our site for professional and enthusiast programmers.

When this migrates to superuser, you might want to say something about what you wanted that grep -r didn't give you. – Jerry Coffin Jan 19 '10 at 16:45
I'm confused - didn't you just answer yourself? what were the results of that line? are you trying to further optimize it? please envision reading this question as someone else, because it's confusing and unclear. – meder omuraliev Jan 19 '10 at 16:46
@other comments: His grep -r statement isn't giving what he wants, unless all the folders end with .php which is probably not the case... – David Oneill Jan 19 '10 at 16:47
I didn't get anything at all. I assume it didn't work because I know it finds it when im on the folder which has the file with this class – Sam3k Jan 19 '10 at 17:48
I found the file by luck. When searching for the file in the folder that has the PHP file with that line, it finds it (no need for -r). If I CD to the root and do the search with recursion -r it doesn't return anything. – Sam3k Jan 19 '10 at 17:50

I'd recommend that you use something like ctags rather than do it with a grep. However, if you want to, you can do something like

 find <top level dir> -name \*.php | xargs grep "class MyClass"

This of course assumes that you have just a single space between class and myClass which might not be true. It's easy to change the regexp to look for any number of whitespaces though.

share|improve this answer

You can try

grep -r --include=*.php "search string" /path/to/dir
share|improve this answer
Works great. I keep forgetting, but fortunately there is always this answer again to help me. :) – GolezTrol Jan 28 '14 at 23:24
@GolezTrol Glad to know that :-) – rahul286 Apr 2 '15 at 13:07

If you use -r, you probably want to recursively search just the current directory:

grep -r 'class MyClass' .

Note the period on the end of the above.

What you told grep was that you wanted it to recursively search every *.php file or directory, but you likely didn't have any directories that ended in a .php extension. The above is the simplest possible way to do the search but also includes files of other types which is especially problematic when you have .svn directories everywhere. If you don't have a lot of other file types, the above is generally sufficient and works well.

Most incarnations of grep don't have a way to specify file extensions that can be searched, so you generally use find in conjunction with it:

find <startDir> -iname '*.php' -exec grep 'class[ \t]*MyClass' {} \;

-iname tells some versions of grep that you want to do a case-insensitive filename search, but many of the non GNU variants of find don't support it, so you could use -name instead.

The -exec option above has the side effect of invoking grep for every file, which is fairly costly.

Still other versions of grep support a + which tells find to append the arguments together to reduce the number of invocations of the executable:

find <startDir> -iname '*.php' -exec grep 'class[ \t]*MyClass' {} + \;

A frequently recommended way to reduce invocations is to use xargs which will fire off grep as few times as possible but as many times as necessary:

find <startDir> -iname \*.php | xargs grep "class[ \t]*MyClass"

xargs is smart enough to pass grep up to the maximum number of arguments supported on the command line, but the above variation doesn't handle certain characters like spaces very well. For example, were 'the file.php' a filename grep would receive 'the' as an argument and 'file.php' as an argument so both wouldn't be found. Instead we use:

find <startDir> -iname \*.php -print0 | xargs -0 grep "class[ \t]*MyClass"

print0 and -0 work together and use arguments followed by a null character so that the argument is completely and unambiguously identified.

f you ever have more than one space after class, or a tab, you might want to allow more characters by changing the regex: class[ \t]*MyClass

This post on StackOverflow has other examples and demonstrates how to exclude certain directories, like .svn directories.

share|improve this answer
This searches all files. Also files that are not .php files. – GolezTrol May 5 '14 at 18:13
I've updated the post to include many other examples. – Kaleb Pederson May 6 '14 at 17:03

Using the ever wonderful ack,

ack --php 'class MyClass'
share|improve this answer
grep -r 'class MyClass' *.php

will search through all the folders with names ending with .php

grep -r 'class MyClass' . | grep .php

will search for class MyClass in all files in all sub folders, then only return the ones that have .php in them.

share|improve this answer

find . -type f -name *.php -print -exec grep \"class MyClass\" {} \; will give you the contents of the line its found on as well as the path to the file.

share|improve this answer

In case the matched files might have arbitrary names, you should consider using -print0

find . -type f -name '*.php' -print0 | xargs -0 grep 'class MyClass'

share|improve this answer
grep -rl 'class MyClass' . --include \*.php

l shows only filename

r is recurive

. searches in current folder

--include limits filename extension

share|improve this answer

I know it says using grep, but my personal favourite is to use ack-grep:

ack-grep "class MyClass"

This runs recursively, lists the file name it finds results in, with line numbers for where they are found, and highlights. To specifically target php files you can run:

ack-grep --type-set php:ext:php "path"

The package has a plethora of options; I'd definitely recommend it (and browsing its associated man page) for doing fancy things.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.