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

link|improve this question

60% accept rate
feedback

4 Answers

up vote 2 down vote accepted

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

link|improve this answer
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. – jscott Aug 20 '10 at 18:59
show 2 more comments
feedback

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.

link|improve this answer
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 – jscott Aug 20 '10 at 20:05
feedback

word = word that you want to search, if you want to search a phrase enclose between two single quotes

grep -iR <word> 

or

 find -name "*.xml" | xargs grep -iR <word>

combining find with grep allow you to make more accurate search combining grep content search with find file search, this is the avalaible option for find :

-name = search file for name (can contain wildcard char)
-iname = same, as name but case insensitive
-atime n = true, if file was accessed n days ago
-amin n = true, if file was accessed n minutes ago
-mtime n = true, if file contents were changed n days ago
-mmin n = true, if file content were changed n minutes ago
-ctime n = true, if file attributes were changed n days ago
-cmin n = true, if file attributes were changed n minutes ago
-regex expression = select files which match the regular expression
-iregex expression = same as above but case insensitive
-empty = select files and directories which are empty
-type filetype = Select file by Linux file types
-user username = Select files owned by the given user
-group groupname = Select files owned by the given group

For Search "Jonathan Appleseed" in /var dir under linux, i do something like this :

find /var | xargs grep -iR 'Jonathan Appleseed'

for search .txt file that contain term "Jonathan Appleseed", you cold do this :

find /var -iname "*.txt" | xargs grep -iR 'Jonathan Appleseed'
link|improve this answer
You either want to do a grep -R on a directory (eg, grep -iR "Jonathan Appleseed" /var ), which isn't clear from your example, or you can use find to list the files, but in that case you don't need the -R parameter to grep. – Daniel Lawson Aug 24 '10 at 22:43
feedback

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.")

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.