In your development projects, you most likely have generated directories, such as node_modules or build directories such as dist or build, that you would like to ignore when doing a recursive search for a pattern. Some grep shell plugins can help with ignoring version control directories such as .git, but it’s always good to know how to explicitly ignore certain directories for search when you need to.

To ignore directories, you can use the --exclude-dir option. In this example, I am searching recursively for “cli-progress” starting from the current directory but excluding the node_modules directory from the search. The dot . at the end of the command is the path to search from, which in this case is the current directory:

grep -r --exclude-dir="node_modules" "cli-progress" . 

The output from this grep command is a good start, but there’s a lot of extra matches in the yarn.lock file that are a little distracting. Let’s ignore the yarn.lock file as well using the --exclude option:

grep -r --exclude-dir="node_modules" --exclude="yarn.lock" "cli-progress" . 

This is much more useful as I have two relevant matches and I can tell at a glance that:

  • We’re using version ^3.12.0 of cli-progress
  • We’re importing cli-progress in front-matter_linter.js script as cliProgress

If I wanted to, I could continue my investigation by performing a search for cliProgress to see where and how it’s used in the script.

Similar Posts