SEARCH

How-To Geek

Find files containing search terms on Ubuntu

To find files containing keywords, linux has a powerful command called grep, which you can use to find the lines inside any file or a list of files. I use this very often to find a function declaration in a set of php files that I’m unfamiliar with.

Let’s say we are looking for the definition of a function called wp_content_filterize, and we don’t know where to start. Let’s try using grep…

grep -i -n ‘function wp_content_filterize’ *

We should see the output right on the console

functions.php:18:function wp_content_filterize($thefilter) {

Now we know that the function definition is found in the file functions.php, on line 18.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/11/06

Comments (11)

  1. Ajo Paul

    find . -name “filenames” -exec grep -i -H -n ‘texttofind’ {} \;

    To search within sub-directories

  2. Mark Olech

    I use windows 2000 and xp. I need a way to search withing a list of text files for a specific number. I want to then take the search results and output the lines of text containing this number to a text file, or possibly an html document that will then open up. Basically, I need to output the results into some form of data that I can easily read. Are there any free programs that I can use to do this or do you have any ideas?

  3. imasdeweb.com

    Now i’m using UBUNTU-LINUX, but before -when i worked in winXP- one of my preferred applications were Agent Ransack (freeware). Here is its offcial webpage:

    http://www.mythicsoft.com/agentransack/

    This application is the only i would like to have in UBUNTU. Once it is installed, you simply do click with secondary button mouse over a directory and click on “Search with Agent Ransack”, and… fantastic: it find very rapidly all files (indicating the exact line) wich contains the string you search!!

    I would like to know if exists some like this for UBUNTU ;)))

    SERGIO R.

  4. imasdeweb.com

    Eyyyy… i found it.
    Great application for developers (indeed for UBUNTU):

    http://regexxer.sourceforge.net

    ;)

    SERGIO

  5. The Geek

    @Sergio:

    That’s a really awesome find! I’ve always wanted an app like that… will be featuring an article about it.

  6. speed

    how do I find some text only in some specific files in a directory, lets say I want grep to look only in .c files only ?

  7. Anderson

    @Ajo :
    In fact you can use -R option on grep to do it recursively (maybe it’s a recent thing, this post is back to 2007, I know)

    grep -R ‘pattern’ *

  8. Nils

    I used grep years ago, but I can’t make it work today:

    I can’t understand why this doesn’t work:

    grep -r keyword *.java

    The error message says that *.java is not a file !
    This used to work 30 years ago.

  9. Sergio M

    After issuing the command you can do
    vi “filename” + “linenumber”
    to open the file in the position of the occurrence

  10. stroem

    use ack-grep

  11. Pavel

    you can also use: grep -R -i -n ‘function wp_content_filterize’ * to search subdirectories

Enter Your Email Here to Get Access for Free:

Go check your email!