Must know Mac/Linux/Terminal grep tips to search content within files
grep is a powerful tool to search content within files on your Mac (Terminal) or Linux systems. The following 3 are handy tips that every developer or user must know to efficiently search content within their files and directories.
Find text only in specific file types/extensions — Returns all the search results.
The --include=\*.py option would only look for the text_to_search within the files with the .py extension.
In addition, the -i option would perform a case insensitive search and the -r option would search the sub-directories.
grep -ir --include=\*.py text_to_search
Only include file names and do not show search results
The -l filter would only list the name of the files containing the search results
grep -ilr --include=\*.py text_to_search
Filter out results
Based on the results, if you notice that there are certain files/folders that you’d like to ignore, you can add a -v option to exclude certain texts on the piped results, and you can add multiple of those at the same time
grep -ilr --include=\*.py text_to_search | grep -v "text_to_ignore1" | grep -v "text_to_ignore2"