AI Learning Hub › Learn AI › Self-Paced Courses › Terminal on Mac: Reading Commands Before You Run Them › Reading and Searching Text
Module 6 ยท Beginner
Reading and Searching Text
Looking inside files without opening an application, searching for text across a folder, and the quoting lesson hiding inside the find command.
By the end
- Read a file without opening an application
- Search for text across a whole folder
- Search for files by name
- Explain why one
findcommand works and an almost identical one fails
This is the module where the terminal starts being obviously worth the trouble. Searching inside a folder full of text is something the command line does quickly and Finder does awkwardly, and it maps directly onto the kind of work this audience actually has.
Before you start, make sure your terminal-practice folder is the one you downloaded and not the one Module 4 left behind. If you created drafts or copied anything into archive or output, delete the folder and unzip the download again. The counts in this module and the next one assume the original contents.
Check you are starting clean
cd ~/terminal-practice ls
Five items: README.md, archive, cases, notes and output. Anything else means you are working in a folder a previous exercise changed.
Looking inside a file
| Command | What it does |
|---|---|
cat file | Prints the whole file. Fine for something short. |
head file | Prints the first ten lines. |
tail file | Prints the last ten lines. |
less file | Opens the file in a scrolling view. Arrow keys move; q quits. |
head and tail both take a number: head -3 file gives you three lines. They are the right choice when you want to know what a file is before committing to reading it.
Try it
cat notes/case-list.txt head -3 cases/opinion-a.txt tail -2 notes/research-notes.txt
Searching inside files
grep finds lines containing some text. Give it the text and a file:
Try it
grep "Jackson" notes/research-notes.txt grep -R "Jackson" . grep -n "Jackson" cases/opinion-a.txt
Three things are going on across those lines. The first searches one file. The second adds -R for recursive, which searches every file in the folder and everything below it — and prefixes each result with the file it came from, which is the part that makes it useful. The third adds -n to number the lines.
Quoting the search text is a habit worth keeping even when it is not strictly required. The moment your search contains a space or an apostrophe, the unquoted version breaks in the way Module 3 showed.
Searching for files
grep looks inside files. find looks for files, by name.
Try it
find . -name "*.txt"
Read that as: starting here (.), find everything whose name matches *.txt. It returns five files from the practice folder, including the ones nested inside cases and notes.
Why the quotes are not optional this time
Take the quotes off and the command fails:
Read this — it does not work
find . -name *.txt zsh: no matches found: *.txt
Look at what answered. Not find — zsh. The command never ran at all.
This is Module 5’s wildcard rule showing up again, and it is the clearest demonstration of it in the course. The shell expands wildcards before handing a command its arguments. With the quotes off, zsh tried to expand *.txt against the current folder, found no .txt files sitting directly there, and stopped. With the quotes on, the pattern survives intact and reaches find, which then does its own matching as it walks through the folders.
Two commands that differ by two characters, and the reason they differ is a step that happens before either of them starts. Once you can see that step, a lot of otherwise baffling command-line behavior becomes legible.
One caveat worth naming: this exact error message is zsh’s. Other shells handle a pattern that matches nothing differently — some pass it through unchanged, which produces a different and arguably more confusing failure. This course is written for zsh, which is what your Mac runs.
Exercise: find the name that appears everywhere
From ~/terminal-practice.
- Use
grep -Rto find every mention ofJackson. How many lines, and in how many files? - Read one of the matching files with
catto see the line in context. - Use
findto list every.txtfile. Count them. - Run the
findagain without the quotes and read the error carefully. Which program produced it? - Search for
docketingwithgrep -R, then search again for justdocket. You get a different number of files. Work out why before opening the answer.
What you should have found
Three lines across three files mention Jackson — both opinions and the research notes. The -R prefixes each result with its filename, which is how you can tell.
Five .txt files: two in cases, three in notes. If you expected three, you were thinking about the folder you were standing in rather than everything underneath it.
The error came from zsh, and you can tell because the message starts with its name. find was never involved.
docketing is in one file; docket is in both. opinion-a.txt has the word docketing; opinion-b.txt has docketed. grep matches the characters you gave it, in order, anywhere in a line — so the shorter stem catches both endings and the longer word catches only one. It is not doing anything clever with word forms.
That matters more in research than it looks. A search for one spelling of a term will quietly miss the others, and grep will report its zero or its one with exactly the same confidence either way.