Searching for files
On this page
You remember saving plan.txt, but two directories could contain it. Listing one directory at a time makes you repeat the same check. You need to ask about the whole practice tree while keeping the search inside it.
By the end, you can find names below a chosen starting directory, require a particular file type, and restrict a match by its path. You can also explain why a filename database might disagree with the files present now.
Before you start
Section titled “Before you start”Finish Practice: Input, Output and Pipes (B09). Keep your B04 lab directory. Create only this disposable tree:
mkdir -p ~/lab/b10/files/notes ~/lab/b10/files/archive ~/lab/b10/files/draft.txtcd ~/lab/b10/filestouch notes/plan.txt notes/meeting.txt archive/plan.txt archive/data.csv .hidden.txtdraft.txt is deliberately a directory. The remaining names are empty regular files; their contents do not matter in this lesson. Use Reset this lesson before repeating the setup. The replay uses learner@rootvolt; your account and host can differ.
Start where the answer could be
Section titled “Start where the answer could be”find . -name 'plan.txt'./notes/plan.txt./archive/plan.txtfind walks the tree beginning at its starting path, here . for the current directory. -name tests each entry’s final name, without its parent directories. The quoted pattern asks for exactly plan.txt. The result includes a path so you can distinguish the two copies.
Keep the starting path explicit. It is the boundary of the search, not merely a label in the output. All searches here start inside ~/lab/b10/files; there is no reason to search / or your whole home directory for these fixtures.
To search only the notes subtree, change the starting path:
find notes -type f -name '*.txt'notes/plan.txtnotes/meeting.txt-type f requires a regular file, rather than a directory or another entry type. Adjacent tests must both succeed: this asks for a regular file whose name matches *.txt. Matching paths are printed automatically when these examples do not specify another action.
The pattern is a filename wildcard, or glob, as in B08.5. It is not a regular expression. Single quotes stop Bash from expanding it before find starts. find then applies the pattern to entries it discovers at every depth below notes.
Separate a matching name from a matching type
Section titled “Separate a matching name from a matching type”Predict first: will the following search include the directory called draft.txt?
find . -name '*.txt'Run it, then compare with your guess
./draft.txt./notes/plan.txt./notes/meeting.txt./.hidden.txt./archive/plan.txtYes. A matching suffix does not prove that an entry is a regular file. The name test alone also accepts draft.txt.
Add the type test when your goal is regular files:
find . -type f -name '*.txt'./notes/plan.txt./notes/meeting.txt./.hidden.txt./archive/plan.txtlearner@rootvolt:~/lab$ cd ~/lab/b10/fileslearner@rootvolt:~/lab/b10/files$ find . -name 'plan.txt'./notes/plan.txt
./archive/plan.txtlearner@rootvolt:~/lab/b10/files$ find . -type f -name '*.txt'./notes/plan.txt
./notes/meeting.txt
./.hidden.txt
./archive/plan.txtlearner@rootvolt:~/lab/b10/files$ Files ~/lab/b10/files
- ~
- lab/b10/files/cwdargument
- archive/
- data.csv-rw-r--r--
- plan.txt-rw-r--r--
- draft.txt/
- .hidden.txthidden-rw-r--r--
- notes/
- meeting.txt-rw-r--r--
- plan.txt-rw-r--r--
Steps (8)
- Type
cd ~/lab/b10/files - Press Enter
- Type
find . -name 'plan.txt' - Press Enter
- Output:
./notes/plan.txt ./archive/plan.txt - Type
find . -type f -name '*.txt' - Press Enter
- Output:
./notes/plan.txt ./notes/meeting.txt ./.hidden.txt ./archive/plan.txt
The Files panel supplies the known tree, while the terminal supplies the selected paths. Searching reads the tree without changing it. Notice that the hidden name .hidden.txt can match find’s name test. Do not transfer Bash’s usual leading-dot glob rule to find.
For directories, use d instead:
find . -type d../draft.txt./notes./archiveThe starting directory is itself tested, so . appears among the directories. A search is not limited to children only. Filesystem traversal order is not a sorting promise; use the paths to judge the result rather than memorizing their order.
In words: start within a selected tree, test each entry’s name, and require the requested type too. An entry is printed only when all the tests used here accept it. This diagram describes selection, not a change to the filesystem.
Test a path rather than its last name
Section titled “Test a path rather than its last name”-name cannot distinguish the two parent directories of plan.txt by itself. GNU find also has -path, which matches the whole path as it is built from the starting argument:
find . -path './archive/*.txt' -type f./archive/plan.txtThe leading ./ matters because this traversal began at .. The pattern accepts the text file under archive, leaving the file under notes out. Here -path’s * can also match slashes; this is not a depth limit. When you simply want everything under archive, starting with find archive is clearer.
Keep both choices separate: the starting path controls where find walks; a path test controls which visited entries it prints. Filtering out an entry does not, by itself, stop traversal into a directory.
Mistakes to recognize
Section titled “Mistakes to recognize”Searching a missing starting directory.
find missing -name 'plan.txt'find: ‘missing’: No such file or directoryThe diagnostic identifies missing, the starting path that cannot be accessed. Check your current directory and the spelling before changing the pattern.
Removing the quotes around a wildcard. Bash may expand it using the current directory before find receives it. Keep '*.txt' quoted even if an unquoted form happens to work in one directory.
Reading an empty result as a broken search.
find . -type f -name '*.pdf'echo "$?"0A successful traversal with no matching files prints no paths and returns status 0. That differs from a search that reports an access error. B09.4’s rule still applies: interpret status according to the particular command.
Good to know
Section titled “Good to know”An optional tool such as locate searches a filename database, a previously collected list of paths. It can answer quickly without walking the tree on each query. Its database may omit a newly created file or retain a deleted name until refreshed. Database coverage and permissions also affect what is returned.
This lesson does not require locate, install it, or show a fabricated database result. Use find for the live practice tree. Even a live search describes what was encountered during traversal; another process can change a file afterward.
Try it yourself
Section titled “Try it yourself”Goal: Find only regular CSV files inside archive, using that directory as the starting path. Leave every file unchanged.
Hint
Combine a regular-file test with a quoted filename pattern. Neither the notes subtree nor the directory draft.txt belongs in this search.
Solution
find archive -type f -name '*.csv'archive/data.csvThe result is the path to archive/data.csv. The suffix selects the name; -type f establishes the entry type independently.
Reset this lesson
Section titled “Reset this lesson”cd ~/lab/b10ls -AR filesrm -r ~/lab/b10/filesRerun Before you start. If the directory is already absent, skip removal.
- An explicit starting path bounds the tree
findsearches. -nametests a final name;-pathtests the path including the starting-path prefix.-type frequires a regular file;-type drequires a directory.- Quote wildcard patterns so
findreceives them unchanged. A database search can be stale.
Next, Searching inside files selects lines from file contents.
Further reading
Section titled “Further reading”- On your system:
man find, sections Name, Full Name, Type, and Exit Status. - GNU Findutils: Finding Files, including filename databases and shell pattern matching.
- Ubuntu 24.04: find(1).