Unix command to search for text string in all files

47,557

Solution 1

Maybe this one?

sudo cd / && grep -rn "hello" *

EDIT: the 'n' option of course is not needed - it simply displays the line numbers and I find it nice. The 'r' option tells grep to perform recursive search within directories.

Solution 2

Use

grep -r 'pattern' /

If your grep supports -r, (GNU grep does); if not use

find / -type f -exec grep 'pattern' {} +

If your find supports -exec with +, otherwise use

find / -type f -printf '%p\0' | xargs -0 grep 'pattern'

If your find supports -printf and your xargs supports -0, or

find / -type f -print0 | xargs -0 grep 'pattern'

If your find supports only -print0 and your xargs supports -0. In all other cases fall back on

find / -type f | xargs grep 'pattern'

This is maximally compatible, with the caveat that certain unusual file names will fail to be grepped and crafted file names could pose a security risk.

Note that you will have to be root in order to be sure of searching all files and that the search will be case-sensitive unless you add -i to grep.

Solution 3

This?

find . -exec grep -H "hello" {} \;
Share:
47,557
MorganR
Author by

MorganR

Updated on July 09, 2022

Comments

  • MorganR
    MorganR almost 2 years

    How do I get unix to search inside ALL files for a the string "hello"?

    I've tried the following but it doesn't work:

    sudo grep "hello" | find / -name "*" 2>/dev/null
    

    Does anyone have any other ideas?

    Thank you.