Finding Files & Text — Terminal

find searches for files by name. find . -name "*.log" finds every file whose name ends in .log in the current folder (.) and every subfolder (folder inside…

A project that grows quickly has hundreds of files, and you often need to find two things: where a file is, and which line mentions something. Picture a library. If you're looking for a book by the title on its cover, you use the catalogue. In the terminal, that catalogue is find, the command that searches by file name. If you're looking for the page that mentions one particular word, you have to open the books. In the terminal, the one that opens the books is grep, the command that searches the text inside files.

The examples below run in Budi's folder ~/web-project. It holds error.log, index.html, script.js, style.css, a logs folder holding app.log, and a src folder holding main.js and utils.js.

find: searching for files by name

budi@laptop:~/web-project$ find . -name "*.log"
./error.log
./logs/app.log
budi@laptop:~/web-project$ find . -name "*.js"
./script.js
./src/main.js
./src/utils.js
budi@laptop:~/web-project$ find . -type d
.
./logs
./src

Why find's pattern needs quotes

This is the same command, but with the pattern written without quotes:

budi@laptop:~/web-project$ find . -name *.log
./error.log

Without quotes, app.log is missing from the result. The shell causes it, not find. Before the command runs, bash replaces *.log with the names that match in the current folder. ~/web-project holds only one match, error.log, so what reaches find is find . -name error.log. With two or more matching files, find gets several names and fails with an error. With no match, bash passes *.log on unchanged, but zsh (the default shell on macOS) refuses the command with zsh: no matches found: *.log. With quotes, the shell leaves *.log as it is, and find is the one matching it in every folder below.

grep: searching the text inside files

logs/app.log has three lines labelled ERROR, and two of them also contain the word error in lowercase. Before you look at the output, guess: how many lines does grep "error" logs/app.log print?

budi@laptop:~/web-project$ grep "error" logs/app.log
2026-10-08 09:02:03 ERROR database connection error
2026-10-08 09:06:45 ERROR email service error: connection refused
budi@laptop:~/web-project$ grep -i "error" logs/app.log
2026-10-08 09:02:03 ERROR database connection error
2026-10-08 09:04:02 ERROR payment timeout
2026-10-08 09:06:45 ERROR email service error: connection refused

Flags you'll use often:

Flag Meaning Example result
-i Ignore upper and lower case 3 lines, not 2
-n Show line numbers 7:2026-10-08 09:02:03 ERROR ...
-c Only count the matching lines grep -c -i "error" logs/app.log prints 3
-v The opposite: lines that don't match
-w Match whole words only -w "log" doesn't match login

grep -r: searching every file with one command

In this example, Budi searches for TODO (a note in code for work that isn't finished):

budi@laptop:~/web-project$ grep -r "TODO" .
./script.js:// TODO: add the order button
./src/main.js:// TODO: fetch the cake list from the API
budi@laptop:~/web-project$ grep -rl "TODO" .
./script.js
./src/main.js

💡 In brief: ripgrep

Many developers install rg (ripgrep), a search tool like grep -r but much faster on big projects. ripgrep also automatically skips files listed in .gitignore (the list of files Git ignores), such as node_modules. The search in VS Code uses ripgrep too. You write it in a similar way: rg "TODO", rg -i "error" logs.

Here are ripgrep and two other tools. This lesson still uses grep -r and find, because both are already on Linux and macOS without installing anything:

Tool When to pick it
rg "TODO" Big projects. Faster, and it skips what is listed in .gitignore.
git grep "TODO" Your project uses Git. It only searches files that Git tracks, so node_modules is left out.
fd -e log A replacement for find when you search files by extension (the end of the name, such as .log): shorter, and it also skips what is listed in .gitignore. On Ubuntu, you install it under the name fd-find, then run it as fdfind.

⚠️ When the search misses or hangs

TL;DR: find searches for files by name. find . -name "*.log" finds every file whose name ends in .log in the current folder (.) and every subfolder (folder inside it). -iname ignores upper and lower case, -type f finds only files, and -type d only folders. The pattern *.log (* means any characters) must be quoted, so the shell doesn't replace it with file names first. grep searches the text inside files and prints every line that contains it: -i ignores upper and lower case, -n line numbers, -c counts, -v lines that don't match, -r every file in a folder, -l file names only.

⚡ Search by name: find

Write:

find . -name "*.log"

Meaning: find searches for files by name. find . starts from the current folder (.) and goes down into every subfolder (folder inside it). * means any characters, so *.log matches every name ending in .log. The quotes are a must, so the shell doesn't replace *.log with file names from the current folder before find runs.

⚡ Search by content: grep

Write:

grep "error" logs/app.log
Guess first: grep prints the lines that contain the text you search for. logs/app.log has three lines labelled ERROR (capital letters), and two of them also contain a lowercase error. How many lines get printed?

Two lines. grep treats upper and lower case as different, so ERROR payment timeout, which has no lowercase error, is left out. Use -i to get all three.

Meaning: grep searches the text inside a file, then prints every line that contains that text, whole.

⚡ Search every file

Write:

grep -rl "TODO" .

Meaning: -r goes into every file in the folder . (the current folder) and its subfolders. -l prints only the names of files that contain TODO (a note for work that isn't finished), without the lines. In short: to search by file name, use find. To search by file content, use grep.

What you will learn