Minimum Grep You Need to Know
You need to find a phrase in over 200 files worth of code. Manual searching is not a feasible option. If you are like me you know about
grep, but it has always made you nervous. It is so powerful and robust that reading the man page was like a tech manual for an engine. Fortunately, getting the benefits of
grep with little pain is easy, once you finally figure it out.
Over the last few months I have had to use
grep more and more, and I would say I use the same type of search 80% of the time. It gets me what I need quickly and efficiently without much fuss.
What is Grep
The best full explanation comes from the
grep man page.
Grep searches the named input FILEs (or standard input if no files are named, or the file name – is given) for lines containing a match to the given pattern. By default, grep prints the matching lines.
My explanation is it finds stuff in files and shows you where it is. It is amazingly useful because of the speed, and by showing the line it found it on along with the file name so you can more easily compare if that is what you need over just a file name.
Grep is very powerful, but I get by with 3 variations day-to-day for most of my needs. First lets look at how to structure your
grep <options> <search-term> <location>
This is important to remember as it can be frustrating when you forget and nothing works.
- options – these are the different flags that can help you get more robust or targeted results back.
- search-term – this takes any pattern/regular expression to match against all the files you are searching
- location – this is where you put a directory or leave blank to search
Grep with Other Commands
If you don’t put in a location it searches
stdin. That is useful if you pipe (
|) a bunch of data to
grep for searching. A mundane example is:
ls -lha | grep buddy
This does a normal
ls -lha and passes the result to
grep. From there it only returns lines that have the word “buddy” in them.
To show the regular expression usage you can do:
ls -lha | grep ^d
This returns only results where the line starts with
d. In the case of
ls it means only directories are returned. As long as you are using the
l option which displays all the data.
Where you will probably spend most of your time is searching for text inside of files. Mostly you will need to know the file and line number of where the word you are looking for is located.
grep -rn hello .
This searches for hello in every file in the current directory and subdirectory. It then shows you the line in the file and the file number it is on. The options are fairly easy to remember as well:
- r – recursively search files
- n – display line numbers
Excluding Directories Sometimes you get too many results or you get results in the folders you don’t want to search in. One of the projects I work on at work has a .svn folder that needs to stay. So I usually have to not include the directory. Fortunately it is easy.
grep -rn --exclude-dir=.svn hello .
Above is about all you need to know to get started using
grep. It is an awesome tool with a lot more features cane you can do some crazy cool searches. It also actually helps you find elusive pieces of code.