CMD+F in your text editor can only get you so far! In this course, we will learn to use grep and find, two powerful command-line utilities, to search through and explore our codebase. Learn to search specific files and folders, using regular expressions to look for -- or ignore! -- particular phrases. We'll cover how to search recursively, get more context on your results, and some simple tricks to make your searches faster and more targeted.
Once you're familiar with grep and find, you'll be able to quickly answer questions such as: Do I use this function anywhere other than my tests? Where does this file get imported? Whether you're working on a new feature, reading a codebase for the first time, or refactoring existing code, grep and find can help you work faster and more effectively.
Learn to use git grep to only search through the tracked files in a git repo. This is especially useful when you want to exclude build artifacts or locally installed dependencies, such as webpack bundles or the node_modules directory. You'll also note that git grep is automatically colorized - we'll see how to get the same coloring effect with grep --color.
Learn to use grep's extended regular expressions to describe more complex patterns. The
+ special characters describe optional patterns. The
? character matches zero or one instance of the preceding term, and the
+ character matches one or more instances of the preceding term. To use these characters, you'll need to either escape them with backslashes or turn on extended regular expressions with the
Describe optional patterns with grep OR using the vertical bar character
|. By using the
| special character, you can write either-or style patterns. In this lesson we'll look for matches on "grey" or "gray". Like the
? characters, the
| character is part of extended regular expressions with grep, so you'll either need to escape it with a backslash, or use the -E flag.
Learn to use the special anchor characters
$ to indicate the beginning and end of lines when writing regular expressions for grep. These line anchors are part of basic regular expressions in grep, so you don't need to escape them.