How can I recursively grep through sub-directories ? | Unix Tutorial |Linux / UNIX Recursively Search All Files For A String

Question :How do i recursively grep (search text files) in unix?

For example, if i want to find the word "hello" in a unix directory /tmp and all its subdirectories in the .log files?  i want the file name, file path, and line number with context.


find /tmp -name \*.log -exec grep pall {} \; -exec ls -l {} \;

The find command you probably know. It searches for files or directorys where you want him to search and to what pattern to match.

So :
"find /tmp" says only start searching in /tmp
"-name \*.log" says to look for all files or directory ending with "log" the \ before the star says to not look for a file or directory named "*log" but to make it a pattern
"-exec grep pall {} \;" says that the result of find in /tmp with the pattern ending on "log" should do the following : "grep hello /tmp/script" so {} \; is replaced with the current result. It's hard to explain the syntax, I think you should just take this as it is and copy the syntax when you need it
"-exec ls -l {} \;" has the same syntax and executes the ls -l command for the current result. If you don't do this last -exec you don't know what file he grep the text from.

No comments:

Post a Comment

Please Provide your feedback here