Skip to content

Commit 0fbaad7

Browse files
committed
Add Grep For Files With Multiple Matches as a unix til
1 parent d57002c commit 0fbaad7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_698 TILs and counting..._
13+
_699 TILs and counting..._
1414

1515
---
1616

@@ -634,6 +634,7 @@ _698 TILs and counting..._
634634
- [Globbing For All Directories In Zsh](unix/globbing-for-all-directories-in-zsh.md)
635635
- [Globbing For Filenames In Zsh](unix/globbing-for-filenames-in-zsh.md)
636636
- [Grep For Files Without A Match](unix/grep-for-files-without-a-match.md)
637+
- [Grep For Files With Multiple Matches](unix/grep-for-files-with-multiple-matches.md)
637638
- [Grep For Multiple Patterns](unix/grep-for-multiple-patterns.md)
638639
- [Hexdump A Compiled File](unix/hexdump-a-compiled-file.md)
639640
- [Jump To The Ends Of Your Shell History](unix/jump-to-the-ends-of-your-shell-history.md)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Grep For Files With Multiple Matches
2+
3+
The `grep` utility is a great way to find files that contain a certain
4+
pattern:
5+
6+
```bash
7+
$ grep -r ".class-name" src/css/
8+
```
9+
10+
This will recursively look through all the files in your css directory to
11+
find matches of `.class-name`.
12+
13+
Often times these kinds of searches can turn up too many results and you'll
14+
want to pare it back by providing some additional context.
15+
16+
For instance, we may only want results where `@media only screen` also
17+
appears, but on a different line. To do this, we need to chain a series of
18+
`greps` together.
19+
20+
```bash
21+
$ grep -rl "@media only screen" src/css |
22+
xargs grep -l ".class-name"
23+
```
24+
25+
This will produce a list of filenames (hence the `-l` flag) that contain
26+
both a line with `@media only screen` and a line with `.class-name`.
27+
28+
If you need to, chain more `grep` commands on to narrow things down even
29+
farther.
30+
31+
See `man grep` for more details.

0 commit comments

Comments
 (0)