logo

How to find all files containing a specific text

1. Using grep (Most Common & Recommended)

grep is the standard tool for searching text patterns within files. Its recursive option (-r or -R) is perfect for this.

  • Basic Search (Case-Sensitive):

    grep -r "your text pattern" /path/to/search
    
    • -r: Recursively search subdirectories.
    • "your text pattern": The exact text or regular expression you're looking for. Use quotes, especially if the pattern contains spaces or special characters.
    • /path/to/search: The directory where you want to start searching (e.g., . for the current directory, /home/user/docs for a specific path).
  • Case-Insensitive Search:

    grep -ri "your text pattern" /path/to/search
    
    • -i: Ignore case differences.
  • Show Only Filenames: (Often what you really want)

    grep -rl "your text pattern" /path/to/search
    
    • -l: List only the names of files containing matches, not the matching lines themselves.
  • Show Line Numbers:

    grep -rn "your text pattern" /path/to/search
    
    • -n: Show the line number for each match.
  • Search for Whole Words Only:

    grep -rw "word" /path/to/search
    
    • -w: Match only whole words (so searching for "cat" won't match "catalog").
  • Exclude Directories:

    grep -r --exclude-dir={node_modules,vendor,.git} "pattern" .
    
    • --exclude-dir: Skip specified directories. Use curly braces {} for multiple directories, separated by commas.
  • Exclude Files:

    grep -r --exclude=\*.log "pattern" .
    
    • --exclude: Skip files matching the glob pattern.

2. Using find + grep (More Control Over File Selection)

This approach uses find to locate files based on various criteria (type, name, size, etc.) and then executes grep on the found files.

  • Basic find + grep (Less Efficient):

    find /path/to/search -type f -exec grep -H "your text pattern" {} \;
    
    • find /path/to/search: Start finding from this directory.
    • -type f: Find only regular files (not directories, links, etc.).
    • -exec command {} \;: Execute command for each file found. {} is replaced by the filename. \; terminates the command.
    • grep -H: -H ensures the filename is printed even if only one file is found.
  • Using find + grep -l (List Filenames Only):

    find /path/to/search -type f -exec grep -l "your text pattern" {} \;
    
    • Uses grep -l to just list the files found by find that contain the pattern.
  • More Efficient find + xargs + grep: (Better for many files)

    find /path/to/search -type f -print0 | xargs -0 grep "your text pattern"
    # Or to list only filenames:
    find /path/to/search -type f -print0 | xargs -0 grep -l "your text pattern"
    
    • -print0: Prints the found filenames separated by a null character (safer for filenames with spaces or special characters).
    • |: Pipes the output of find to xargs.
    • xargs -0: Reads null-separated items and executes the following command (grep) with those items as arguments. This runs grep fewer times than -exec \;.

3. Using Modern Alternatives (Often Faster)

Tools like ripgrep (rg) and The Silver Searcher (ag) are designed for speed and often have better defaults (like respecting .gitignore and recursing automatically).

  • Using ripgrep (rg): (Highly Recommended if installed)

    # Basic search (recursive by default)
    rg "your text pattern" /path/to/search
    
    # List filenames only
    rg -l "your text pattern" /path/to/search
    
    # Case-insensitive
    rg -i "your text pattern" /path/to/search
    
    # Search specific file types
    rg -t py "class MyClass" /path/to/search
    
    • rg is extremely fast and respects .gitignore and hidden file rules by default.
  • Using The Silver Searcher (ag): (Also very fast)

    # Basic search (recursive by default)
    ag "your text pattern" /path/to/search
    
    # List filenames only
    ag -l "your text pattern" /path/to/search
    
    # Case-insensitive (default is smart case - ignore if pattern is lowercase, sensitive otherwise)
    ag -i "your text pattern" # Force case-insensitive
    
    • ag is similar to rg in speed and features.

Choosing the Right Method:

  • For general use and simplicity: grep -rl "pattern" . is usually the easiest and best starting point.
  • If you need complex file filtering before searching content: Use find combined with xargs grep.
  • For speed, especially in large codebases: Install and use rg or ag. They are significantly faster.

Remember to replace "your text pattern" and /path/to/search with your actual search term and target directory.