Day 34 - Recursive grep

10 May 2017

Grep argument of the day => -r

It searches a pattern through sub-directories.

$ git clone git@github.com:iadvize/devops-tip-of-the-day.git
$ grep -n -r 'grep' devops-tip-of-the-day/ | grep -v \.git
6:devops-tip-of-the-day//_posts/2017-03-21-day-08-tree-command.markdown:61:Pretty usefull for grep parsing:
7:devops-tip-of-the-day//_posts/2017-03-21-day-08-tree-command.markdown:64:$ tree -Cfi | grep .js$
8:devops-tip-of-the-day//_posts/2017-04-04-day-18-ps-command.markdown:57:$ ps aux | grep sleep
9:devops-tip-of-the-day//_posts/2017-04-04-day-18-ps-command.markdown:59:root     14096  0.0  0.0 2423376 272  ?        R+   11:35AM 0:00.00 grep --color -i sleep
10:devops-tip-of-the-day//_posts/2017-04-04-day-18-ps-command.markdown:61:$ ps aux | grep sleep
11:devops-tip-of-the-day//_posts/2017-04-04-day-18-ps-command.markdown:62:root     14096  0.0  0.0 2423376 272  ?        R+   11:35AM 0:00.00 grep --color -i sleep
12:devops-tip-of-the-day//_posts/2017-04-06-day-20-unix-signals-fork-behavior.markdown:11:$ ps -axfo pid,ppid,tid,comm | grep nginx
13:devops-tip-of-the-day//_posts/2017-04-06-day-20-unix-signals-fork-behavior.markdown:31:$ ps -axfo pid,ppid,tid,comm | grep nginx
14:devops-tip-of-the-day//_posts/2017-05-04-day-31-grep-exclude.markdown:3:title:  "Day 31 - grep - Exclude results"
15:devops-tip-of-the-day//_posts/2017-05-04-day-31-grep-exclude.markdown:8:Grep is a powerful command. You can reverse the grep command, by excluding a pattern with -v argument:
16:devops-tip-of-the-day//_posts/2017-05-04-day-31-grep-exclude.markdown:23:$ cat foobar.txt | grep cd
17:devops-tip-of-the-day//_posts/2017-05-04-day-31-grep-exclude.markdown:30:$ cat foobar.txt | grep -v cd
18:devops-tip-of-the-day//_posts/2017-05-04-day-31-grep-exclude.markdown:38:$ cat foobar.txt | grep a | grep -v cd
19:devops-tip-of-the-day//_posts/2017-05-05-day-32-grep-count-occurence.markdown:3:title:  "Day 32 - Grep: counting occurences"
20:devops-tip-of-the-day//_posts/2017-05-05-day-32-grep-count-occurence.markdown:8:The next useful grep argument is -c. It counts the number of time a pattern is matched:
21:devops-tip-of-the-day//_posts/2017-05-05-day-32-grep-count-occurence.markdown:12:$ cat /etc/passwd | grep -c /bin/bash
22:devops-tip-of-the-day//_posts/2017-05-05-day-32-grep-count-occurence.markdown:15:$ cat /etc/passwd | grep /bin/bash
23:devops-tip-of-the-day//_posts/2017-05-09-day-33-grep-line-number.markdown:3:title:  "Day 33 - Grep: line number"
24:devops-tip-of-the-day//_posts/2017-05-09-day-33-grep-line-number.markdown:8:Grep argument of the day => -n.
25:devops-tip-of-the-day//_posts/2017-05-09-day-33-grep-line-number.markdown:15:$ cat /etc/passwd | grep -n /bin/bash
26:devops-tip-of-the-day//_posts/2017-05-10-day-34-grep-recursive.markdown:3:title:  "Day 34 - Recursive grep"
27:devops-tip-of-the-day//_posts/2017-05-10-day-34-grep-recursive.markdown:8:Grep argument of the day => -r