Use Grep for Fast Search from the Command Line

24 minutes

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.

pro-course-rss-logo

PRO RSS Feed

Search the contents of files using grep

Search directory contents recursively using grep

Use find to search for filename patterns

Search matching files by combining find and grep with xargs

Search the contents of a git repository with git grep

Show context around matches using grep

Search for basic patterns using grep

Use grep's extended regular expressions to find complex patterns

Search for optional patterns with grep OR

Specify line beginning and end in patterns using grep

Match classes of characters using bracket expressions with grep

Search with groups using grep

Find matches excluding a pattern with grep

otherjs tutorial about Search the contents of files using grep

Search the contents of files using grep

2:15 otherjs

Learn the basic syntax for using grep to search the contents of a single file or files. It's like CMD+F, but better! You'll be able to quickly see all matching results in a file.

otherjs tutorial about Search directory contents recursively using grep

Search directory contents recursively using grep

1:41 otherjs

Sometimes you'll be looking for a string, but won't know which file it's in; or you'll want to find all usages of it within a directory. Learn how to use grep -r to recursively search all the files in a directory.

otherjs tutorial about Use find to search for filename patterns

Use find to search for filename patterns

1:33 otherjs

Learn how to use find to identify filenames matching specified patterns. We'll use find to identify all .jsx files in a directory, or all test files in a directory.

otherjs tutorial about Search matching files by combining find and grep with xargs

Search matching files by combining find and grep with xargs

2:20 otherjs

Learn to use the xargs Unix command to combine find and grep. First we can use find to identify which files we want to search; then we can use xargs to tell grep which files to search through.

otherjs tutorial about Search the contents of a git repository with git grep

Search the contents of a git repository with git grep

1:46 otherjs

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.

otherjs tutorial about Show context around matches using grep

Show context around matches using grep

1:59 otherjs

By default, grep displays the file path and the line where a match is found. Learn to use the flags -A, -B, -C, and -n to get more context, and view line numbers as well as the lines surrounding a pattern match.

otherjs tutorial about Search for basic patterns using grep

Search for basic patterns using grep

2:09 otherjs

By using wildcards and glob patterns with the special characters . and *, you can create regular expressions that describe what kind of text you're looking for, instead of just plaintext strings.

otherjs tutorial about Use grep's extended regular expressions to find complex patterns

Use grep's extended regular expressions to find complex patterns

2:35 otherjs

Learn to use grep's extended regular expressions to describe more complex patterns. The ? and + 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 -E flag.

otherjs tutorial about Search for optional patterns with grep OR

Search for optional patterns with grep OR

1:07 otherjs

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 + and ? 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.

otherjs tutorial about Specify line beginning and end in patterns using grep

Specify line beginning and end in patterns using grep

1:18 otherjs

Learn to use the special anchor characters ^ and $ 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.

otherjs tutorial about Match classes of characters using bracket expressions with grep

Match classes of characters using bracket expressions with grep

2:21 otherjs

You can describe classes of characters using bracket expressions. We'll use expressions such as [a-zA-Z] and [0-9] to search for alphabetic and numeric characters with grep.

otherjs tutorial about Search with groups using grep

Search with groups using grep

2:12 otherjs

By grouping your searches with parentheses, you can create even more complex searches. Grouping lets you separate out different parts of your regular expression, and apply special characters like +, ?, and * to groups of search terms.

otherjs tutorial about Find matches excluding a pattern with grep

Find matches excluding a pattern with grep

0:59 otherjs

What happens when you want to find things that don't contain a pattern? The -v flag lets you use grep in inverse mode. We'll use grep -v against find's output to exclude files in node_modules.

Presented by:

Bonnie Eisenman

Working on Scala and distributed systems stuff at Twitter. Author of Learning React Native.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?