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

bobobobo
  • 769
  • 6
  • 14
  • 26

3 Answers3

2

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

jscott
  • 24,204
  • 8
  • 77
  • 99
Nikolaidis Fotis
  • 1,994
  • 11
  • 13
  • No double-quotes required? – bobobobo Aug 20 '10 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 – Nikolaidis Fotis Aug 20 '10 at 18:32
  • I had luck with `grep -RE "Jonathan Appleseed" .` – bobobobo Aug 20 '10 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 '10 at 18:56
  • @bobobobo See also [this SF question](http://serverfault.com/questions/69283/when-to-use-single-quote-double-quote-in-grep). – jscott Aug 20 '10 at 18:59
  • I'd recommend including the `-I` and `-H` flag as well. `-I` prevents binary files from matching. Useful when recursing, because you never know what massive binary files might be out there. `-H` includes the file name. – Christopher Karel Aug 20 '10 at 19:15
  • `-E` is not "for case sensitive". Searches using `grep` are case sensitive by default. The `-E` switch makes the search pattern an extended regular expression. The `-H` switch isn't usually necessary for a recursive search since it's on by default for searches involving multiple files. – Dennis Williamson Aug 20 '10 at 23:05
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.

mpez0
  • 1,492
  • 9
  • 9
  • Yes, the bar! Thanks for using it. What does `|` mean? – bobobobo Aug 20 '10 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](http://tldp.org/LDP/abs/html/io-redirection.html) – jscott Aug 20 '10 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.")

Slartibartfast
  • 3,265
  • 17
  • 16