1

Say I want to find all files that mention "Jonathan Appleseed" in a Linux system.

I see examples using grep, but I can't quite grep yet how to search (all directories from HERE). So I want to look in everything below /var/, for example

3 Answers 3

2

haha. It will take hours :> in any case .... grep -RE 'Jonathan Appleseed' R is for recursive, and E for case sensitive

7
  • No double-quotes required?
    – bobobobo
    Aug 20, 2010 at 18:30
  • Yes, they are needed. I thought i included them (They may be deleted because i choose to present it as code. Anyway. Yes, they are needed Aug 20, 2010 at 18:32
  • I had luck with grep -RE "Jonathan Appleseed" .
    – bobobobo
    Aug 20, 2010 at 18:50
  • 1
    @bobobobo Yes, that will work for a simple grep. Please be aware that using double quotes will cause the shell to expand variables before handing it off to grep. Single quotes will not do this and will allow the use of regex.
    – jscott
    Aug 20, 2010 at 18:56
  • @bobobobo See also this SF question.
    – jscott
    Aug 20, 2010 at 18:59
1

If your grep doesn't have the -R option,

find /var -type f -print | xargs egrep 'Jonathan Appleseed'

will generally do what you're asking.

2
  • Yes, the bar! Thanks for using it. What does | mean?
    – bobobobo
    Aug 20, 2010 at 19:23
  • 2
    It is the "pipe". It pipes the output (stdout) of one thing to the input (stdin) of something else. The pipe is just one part of redirection
    – jscott
    Aug 20, 2010 at 20:05
0

I want to find all files that mention "Jonathan Appleseed" in a Linux system.

You're looking for:

grep -l -r "Jonathan Appleseed" /

If you want to run a command on all matching files, I would suggest:

grep -l -z -r "Jonathan Appleseed" / | xargs -0 <your command here>

Note that -l means show only the filename (not matching text), -r means recursive, and -z (if you choose to use it) means the file names are null ("\0") terminated rather than terminated with a carriage return. This means xargs can handle filenames with spaces, tabs, and carriage returns in the name more readily.

I also am passing / to indicate that grep should start at the root of the filesystem ("all files... in a Linux system.")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .