You know a file exists somewhere in the project. You do not know which folder.
ls will not help — it shows one folder at a time. You need something that walks down through everything.
find
find . -name "*.js"
./src/app.js
./src/utils.js
Read the command in three pieces:
find— the command.— where to start, in this case the current folder-name "*.js"— what to match
find starts at the place you named and goes down through every folder inside it, at every depth, printing the that match.
Quick check
In `find docs -name README.md`, what does docs mean?
The star
* in a pattern means “any characters, including none”:
| Pattern | Matches |
|---|---|
*.txt | anything ending in .txt |
app.* | app.js, app.css, app.json |
*test* | any name with test somewhere in it |
Tip
Quote the pattern: -name "*.js", not -name *.js. Without quotes, the tries to expand the star before find ever sees it, and you get results that depend on what happens to be sitting in your current folder. Quoting removes the surprise.
Quick check
Which pattern finds notes.txt, notes.md and notes.pdf?
What find does not do
find matches names. It does not look inside files at all. Searching for a word inside your files is a different command — grep — and that is the next lesson.
Keeping the two apart saves confusion later:
find— where is the file called something like this?grep— which files contain this text?
Note
On a real machine find / searches the entire disk, which is slow and prints permission errors for system folders you are not allowed to read. Start from a project folder, not from the root.
Quick check
You need to know which file contains the phrase 'database timeout'. Which tool?
Try it
The project has files at two different depths.
ls— notice you only see the top level:docs,src,todo.txt.find . -name "*.js"— both JavaScript files turn up, including the ones nested insidesrc.find . -name "*.txt"— three text files, from two different folders.
Compare step 1 with step 3. ls showed you one .txt file; find showed you all three.