From 0fbaad7a09b3ae750009a936e0b7d66040cc57f1 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 15 Aug 2018 12:56:01 -0500 Subject: [PATCH] Add Grep For Files With Multiple Matches as a unix til --- README.md | 3 +- unix/grep-for-files-with-multiple-matches.md | 31 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 unix/grep-for-files-with-multiple-matches.md diff --git a/README.md b/README.md index cbec074..8a28fc4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_698 TILs and counting..._ +_699 TILs and counting..._ --- @@ -634,6 +634,7 @@ _698 TILs and counting..._ - [Globbing For All Directories In Zsh](unix/globbing-for-all-directories-in-zsh.md) - [Globbing For Filenames In Zsh](unix/globbing-for-filenames-in-zsh.md) - [Grep For Files Without A Match](unix/grep-for-files-without-a-match.md) +- [Grep For Files With Multiple Matches](unix/grep-for-files-with-multiple-matches.md) - [Grep For Multiple Patterns](unix/grep-for-multiple-patterns.md) - [Hexdump A Compiled File](unix/hexdump-a-compiled-file.md) - [Jump To The Ends Of Your Shell History](unix/jump-to-the-ends-of-your-shell-history.md) diff --git a/unix/grep-for-files-with-multiple-matches.md b/unix/grep-for-files-with-multiple-matches.md new file mode 100644 index 0000000..60d1b0f --- /dev/null +++ b/unix/grep-for-files-with-multiple-matches.md @@ -0,0 +1,31 @@ +# Grep For Files With Multiple Matches + +The `grep` utility is a great way to find files that contain a certain +pattern: + +```bash +$ grep -r ".class-name" src/css/ +``` + +This will recursively look through all the files in your css directory to +find matches of `.class-name`. + +Often times these kinds of searches can turn up too many results and you'll +want to pare it back by providing some additional context. + +For instance, we may only want results where `@media only screen` also +appears, but on a different line. To do this, we need to chain a series of +`greps` together. + +```bash +$ grep -rl "@media only screen" src/css | + xargs grep -l ".class-name" +``` + +This will produce a list of filenames (hence the `-l` flag) that contain +both a line with `@media only screen` and a line with `.class-name`. + +If you need to, chain more `grep` commands on to narrow things down even +farther. + +See `man grep` for more details.