How to Use grep Command in Bash to Quickly Find Substrings within File System/Cloned GH Repo

The aim of this pagešŸ“ is to explain searching for a substring in files with a specific extension and printing additional lines after the match using the grep command in Unix-based systems. It started with the task of searching through a complex GitHub repo containing around fifty datamodeling jobs and I needed to filter those that have a certain Redshift host listed in a configuration file.

Pavol Kutaj
2 min readFeb 23, 2024
  • of course, the command was generated by Warp AI Chatbot within seconds, but I am still convinced that it is great to automate and memorize these as one encounters them often in script files, etc. and why should I run to tools to explain if I can just read through the code smoothly

Command to list just the files

grep -rl "redshift.example.com" --include="*.yml" .

Command to list the contents with four additional lines

grep -r -A 4 "redshift.example.com" --include="*.yml" .
  • The -r flag in grep is used for recursive search within directories.
  • The -l flag in grep is used to return filenames containing the match.
  • The --include flag in grep is used for filtering files based on globbing patterns.
  • The -A 4 flag in grep is used to print not only the matching line but also the specified number of lines after the match.

ANKI

Question:
What is the purpose of the -r flag in the grep command?
Answer:
The -r flag in the grep command is used for recursive search within directories.
Question:
What does the -l flag do in the grep command?
Answer:
The -l flag in the grep command is used to return filenames containing the match.
Question:
How is the --include flag used in the grep command?
Answer:
The --include flag in the grep command is used for filtering files based on globbing patterns.
Question:
What does the -A 4 flag do in the grep command?
Answer:
The -A 4 flag in the grep command is used to print not only the matching line but also the specified number of lines after the match.

--

--

No responses yet