Q1What is shown when you run grep error log.txt?
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 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
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.
| Option | Meaning | Example |
|---|---|---|
-i | Don't distinguish uppercase and lowercase | grep -i error log.txt |
-n | Add a line number in front of each match | grep -n error log.txt |
-v | Show the lines that don't match | grep -v info log.txt |
-r | Search a directory recursively | grep -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)
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.
-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
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 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
Knowledge Check
Answer each question one by one.
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?