Learn by reading through in order

Text Search — grep

Learn the basics of finding lines containing a string, plus when to use -i to ignore case, -n for line numbers, and -r for recursive search — hands-on in a browser terminal.

Finding a String — grep

grep is the command that pulls out and shows only the lines in a file that contain a given string.

Use it as grep search-string filename, and if there are matching lines, they print to the screen as-is.

Use it to find just the lines you need out of a large log.

grep pulls out only matching lines
applebananacherrygrep banana fruits.txtshow only the matching bananaother lines don't appear
grep pulls out only the lines containing the given string, and doesn't output the rest.
echo 'error: disk full' > log.txt   # create line 1
echo 'info: started' >> log.txt     # append
echo 'error: timeout' >> log.txt    # append
grep error log.txt                  # the 2 lines containing error
grep info log.txt                   # the 1 line containing info

① Create line 1 with echo 'error: disk full' > log.txt, then append with echo 'info: started' >> log.txt and echo 'error: timeout' >> log.txt to make a 3-line material file.

② Use grep to show only the lines containing error from log.txt.

③ Confirm that every line shown contains error. (Run it correctly and an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

Ignore Case — -i / Line Numbers — -n

grep distinguishes uppercase from lowercase.

Add -i to search without that distinction, so you can catch both Error and error.

Add -n and the line number is shown in front of each matching line, so you can tell where in the file it is.

-i and -n can be combined and applied at once, like grep -in error app.txt.

OptionMeaningExample
-iDon't distinguish uppercase and lowercasegrep -i error log.txt
-nAdd a line number in front of each matchgrep -n error log.txt
-vShow the lines that don't matchgrep -v info log.txt
-rSearch a directory recursivelygrep -r TODO src
echo 'Error in disk' > app.txt   # create line 1
echo 'error in app' >> app.txt   # append
echo 'ok' >> app.txt             # append
grep -i error app.txt            # the 2 lines Error and error
grep -n error app.txt            # 2:error in app (with line number)

① Create line 1 with echo 'Error in disk' > app.txt, then append with echo 'error in app' >> app.txt and echo 'ok' >> app.txt to make a material with mixed case.

② First search for error with grep and the option that ignores case, and confirm the Error line is caught too.

③ Next run the same search with the option that shows line numbers, and confirm a line number appears in front of each match.

Linux console
0 / 5 completed
Loading Linux Terminal...

Excluding Non-Matching Lines — -v

Add -v and grep shows the lines that don't contain the given string.

Whereas ordinary grep keeps the matching lines, -v removes the matching lines and outputs the rest.

Use it when you want to read a log with unneeded lines (like info or debug) removed.

grep -v keeps the non-matching lines
error: disk fullinfo: startederror: timeoutgrep -v info log.txtlines with info are excludedshow the remaining 2 error lines
-v excludes the lines containing the given string and shows only the rest.
echo 'error: disk full' > log.txt   # create line 1
echo 'info: started' >> log.txt     # append
echo 'error: timeout' >> log.txt    # append
grep -v info log.txt                # the 2 lines that don't contain info

① Create line 1 with echo 'error: disk full' > log.txt, then append with echo 'info: started' >> log.txt and echo 'error: timeout' >> log.txt to make a 3-line material file.

② Use grep with the option that keeps non-matching lines to show only the lines from log.txt that don't contain info.

③ Confirm that the lines shown are the 2 error lines and the info line is gone.

Linux console
0 / 4 completed
Loading Linux Terminal...

Searching a Directory Recursively — -r

When you want to search not just one file but every file under a directory, add -r and pass a directory instead of a filename.

It walks down into subdirectories recursively and shows matching lines in the form filename:line.

grep -r walks a directory recursively
grep -r TODO srcsrc/src/a.txtTODO: fix latersrc/sub/src/sub/b.txtTODO: review
grep -r starts from the src you pass and walks down into the subdirectory src/sub to search every file.
mkdir -p src/sub                       # create the material directory
echo 'TODO: fix later' > src/a.txt     # 1 file in the parent
echo 'TODO: review' > src/sub/b.txt    # 1 file in the subdirectory
grep -r TODO src                       # search recursively under src

① Create a material tree with a subdirectory using mkdir -p src/sub.

② With echo 'TODO: fix later' > src/a.txt and echo 'TODO: review' > src/sub/b.txt, create files in both the parent and the subdirectory.

③ Search for TODO under the src directory with grep and the recursive-search option, and confirm the lines from both files appear.

Linux console
0 / 4 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What is shown when you run grep error log.txt?

Q2Which option do you add to grep to search without distinguishing uppercase and lowercase?

Q3How does grep behave when you add -r and pass a directory?