How to search/find specific string in Linux and Solaris

In this post we are going learn about how to find search string in files and folders in Linux and Solaris machines. The list of techniques are describe below
grep -rnw 'directory' -e "pattern"
-r is recursive, -n is line number and -w stands match the whole word. Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:
grep --include={*.c,*.h} -rnw 'directory' -e "pattern"
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:
grep --exclude=*.o -rnw 'directory' -e "pattern"
Above will exclude searching all the files ending with .o extension. In post we are going to learn about how to search string in all files in Solaris. Solaris 10 includes grep utility it won't work recursively like in many Linux flavours. We can search strings recursively using the following
$ grep 'search string' `find . -name '*'`
It works well in same directory. We can search strings including sub folders using the following approach
$ find . | xargs grep 'search string'
This is very useful to search strings, but It will take little longer time.
SHARE

    Blogger Comment
    Facebook Comment

1 comments :