3/02/2009

Untold Best Practices - Grep/Search Text recursively


grep recursively,grep text recursively,search text in sub directory on linux, search text in sub directory on windows, search text recursively on linux, search text recursively on windows,
find text in sub directory on linux, find text in sub directory on windows, find text recursively on linux, find text recursively on windows,
grep text in sub directory on linux, grep text in sub directory on windows, grep text recursively on linux, grep text recursively on windows, grep command minus -r option doesn't work


Searching for a specific text is very much required almost every day in a software programmers life. I have seen that many tools and IDE still fail to provide good performance in doing this. Below mentioned practices are my personal preferences from long time and I would like to share it with my readers. Let me know if you have a better option then the ones mentioned below.

Searching Text inside files on your Windows Desktop: Textpad is always my choice
This is not about promoting Textpad editor, but I still use Textpad for searching text inside files. Its the fastest text based searching tool I have seen ever. Specially when you are looking for all list of files which contain a specific text it can give you real quick results. It does searches on even binary files and find the text you are looking for. You can not even compare its performance with Windows search. The last time I did a search for text inside all files on Windows search it never returned back. Most of my development activity happen in Eclipse but when I have to search a specific text in all files I always go to Textpad and search the parent directory recursively and results are phenomenal. I know many of my colleagues who also prefer the same way.


Searching Text recursively on Unix/Linux/Solaris Platforms:
This is the most important command I had been using from several years and have shared with many of my fellow developers who don't feel much comfort on Unix commands. Most of the people who have elementary knowledge of Unix would know grep command for searching text inside a file or all files in a folder. I have never found an option for grep command to search text recursively in sub-folders(share it with me if you already know about it). To search files by name you can use find command, which can search files recursively.

Below mentioned commands can search (or grep) text in sub directories on Linux or other Unix flavors. I have not tested them on Solaris so you may want to post your feedback on how it works on Solaris platform.

Now the command I am referring is a combination of find and grep command with xargs joined with pipe. So this command can be used to search any text inside all sub folders for mentioned directory <DIR>
find <DIR> -name "*" | xargs grep "my text"

The xargs command allows output of find command to be passed as parameter to the grep command and as list of files is searched by find command, grep command looks for respective text inside that file.

You can also use [ -i ] option in grep command if you want to ignore the case of text being searched. So the command will look like this.

find <DIR> -name "*" | xargs grep -i "my text"



One more option on grep command can also be used to search recursively on Linux, I am not sure if this works on other flavors of unix.

grep -irH 'my text' <DIR>

In above commands example
- the option "i" is for ignoring the case while searching,
- the "r" option is to search recursively, which may not work on all flavors on unix. Please post your comments about this option if its works on Solaris.
- the "H" option is to print the file name with the match.

Let us know if you also know something small like this which can improve day to day life of a developer. If you have Solaris OS then please don't forget to post your response about working of all of the above commands.

2 comments:

  1. I have used the find command on several flavors of unix it seems it the quotes doesnt work consistently in all unix flavors. some places using only * works some places using single quote ' works.

    ReplyDelete
  2. AnonymousMay 14, 2009

    Surely:

    find directory_name -type f | xargs grep ....

    ReplyDelete

Got something to say? Don't hold it! Tell it to us.

You Might Like

.....