Register to get access to free programming courses with interactive exercises

Grep CLI fundamentals

The word "grepping" is among the most popular terms used by developers. It comes from the console utility grep (global regular expression print), which searches a file or files for specific text. "Grepping" for developers is the same as "Googling" for internet users. As a rule, the files with the source code or logs during debugging are the ones that are grepped.

man grep

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]

PATTERN — is what is being searched for, it doesn't necessarily have to be a specific string, it can be a specific pattern (see regular expressions). FILE — the path to the file to search in

# Search for all lines in the .bashrc file that contain the word "aliases"
grep aliases .bashrc

# enable color support of ls and also add handy aliases
# some more ls aliases
# ~/.bash_aliases, instead of adding them here directly.
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases

In the example above, grep found 5 lines. The lines found are displayed in the same order as they appear in the source file. In some situations, it may be important to see not only the string itself, which contains a substring, but also what's around it. The number of adjacent lines to be output is controlled by the -B, -A and -C options. The first determines the number of lines to display before (-B, --before-context), the second after (-A, --after-context), and the third before and after simultaneously (-C, --context). Below is an example of using -C with a value of 1. This means that for each line found, one line above and one line below will be printed too.

grep -C 1 aliases .bashrc

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
--

# some more ls aliases
alias ll='ls -alF'
--
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Sometimes we don't know which file contains what we're looking for, but we know the directory where the file lies. Two changes need to be made in this situation:

  1. Add an -R option, which tells the terminal to search inside the directory (recursively, i.e., including all subdirectories).
  2. Specify the path to the directory, not the file.
grep -R bashrc .

./.profile:    # include .bashrc if it exists
./.profile:    if [ -f "$HOME/.bashrc" ]; then
./.profile: . "$HOME/.bashrc"
./.bash_history:du -sh .bashrc
./.bash_history:stat .bashrc
./.bash_history:stat -h .bashrc
./.bash_history:file .bashrc
./.bash_history:stat .bashrc
./.bash_history:cat .bashrc
./.bashrc:# ~/.bashrc: executed by bash(1) for non-login shells.
./.bashrc:# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
./.bashrc:# sources /etc/bash.bashrc).

This search outputs the file in which the string was found. If you add the n option, the line number will also be displayed.

grep -Rn bashrc .

./.profile:13:    # include .bashrc if it exists
./.profile:14:    if [ -f "$HOME/.bashrc" ]; then
./.profile:15:  . "$HOME/.bashrc"
./.bash_history:56:du -sh .bashrc
./.bash_history:57:stat .bashrc
./.bash_history:58:stat -h .bashrc
./.bash_history:60:file .bashrc
./.bash_history:61:stat .bashrc
./.bash_history:63:cat .bashrc
./.bashrc:1:# ~/.bashrc: executed by bash(1) for non-login shells.
./.bashrc:109:# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
./.bashrc:110:# sources /etc/bash.bashrc).

Recommended materials

  1. Locating files

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY