Get greppin': Useful Directory Searching Commands
What is grep? How would one grep?
Letβs find out. In your terminal, run:
man grep
π¨ Tip: Man (for manual) is a helpful command
you can run to display user manual of any commands on the terminal.
According to the manual, grep
is a (Linux) tool to help search files through patterns and regular expressions. There are also several other variations, such as egrep
and fgrep
, with more specific functionality.
Essentially, grep
is a low-level search tool from the command line. It can be quite a powerful utility tool once you get familiar with the command options.
There are more flags other than the ones listed below, but here are some of the flags I found useful in day-to-day programming work.
Flags to get familiar with #
grep -i "gains" *
Searches in current directory, ignoring case sensitivity. (Grep is case sensitive by default)
grep -r "gains" *
Searches recursively in subdirectories
grep -l "gains" *
Lists file paths of files containing the matched keyword
grep -c "gains" *
Counts the number of matches of the word for each file
grep βm2 "gains" *
Returns the first two matches of the keyword
grep "gains" * --colour
Highlights the search word in the returned results
These flags can be used collectively in a single command: #
grep -r -i "gains" * --colour
Recursively searches for the keyword without case sensitivity and return results in colour
Using regular expressions #
In any of the cases above you can replace your search word with a regex (and an extended regex in egrep
).
grep -r -m3 "/gains/\d" *
Searches for first 3 matches for the pattern β/gains/β followed by any digit
π¨ Tip: Remember to use quotation marks whenever there is a space
or a symbol in a search pattern. Personally, I use quotes just
to be safe regardless.
- Next: Packwerk at Shopify