shio

~/shio.sh/learn/searching $ ./read

chapter 06 · learn the terminal

Searching (grep, find)

grep finds text inside files. find finds files by name. Both are how you stop scrolling and start looking.

$ grep — find text inside files

grep takes a pattern and one or more files, and prints every line that contains the pattern. It is, without exaggeration, one of the most-used commands in the world.

grep salt notes.txt

That prints every line in notes.txt that contains the word "salt." Useful flags:

  • -i — case-insensitive ("Salt" matches too)
  • -n — show line numbers

try: grep salt notes.txt

$ find — locate files by name

find walks through folders looking for files. The shape is: find <where> -name <pattern>.

find . -name "*.txt"

That finds every file ending in .txt from the current directory down. The * is a wildcard — it matches any sequence of characters.

try: find . -name "*.txt"

You can also pass -type f (only files) or -type d (only directories).

$ a small rule of thumb

Looking for files by name: find.
Looking for text inside files: grep.

In the next chapter, you'll learn how to combine them — and how that combination is the single biggest unlock the terminal offers.

~/shio.sh/learn/searching $ cd ..