grep and find on MacOS
6/21/20
My last post was about some useful bash commands like grep
and find
. I just learned that while Mac OS is a unix based system, these commands work a tad differently. For example, on Linux
$ grep "baseball" -r
needs to be changed to
$ grep "baseball" -r .
Otherwise you get a very spooky grep: warning: recursive search of stdin
. The .
at the end is needed to specify the location to search recursively. A similar thing happens with find
.
For example, on Linux…
$ find -regex ".*_names"
turns into
$ find . -regex ".*_names"
Again, we need the .
in order to specify the location of the search.
Expanding search results in grep
The grep
usage I showed above only shows the line on which the match occurs. By using the -B
and -A
options we can specify how many lines before and after we want to see. For example:
$ grep -B 1 -A 1 "baseball" searchme.txt
Here is a sentence before.
baseball
This is the sentence after.
And now grep will show 1 sentence before (-B
) and 1 sentence after (-A
) the match.
Onward to more command line knowledge…