Skip to content

Searching for files

Module
Finding Files and Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

Finish Practice: Input, Output and Pipes (B09). Keep your B04 lab directory. Create only this disposable tree:

Terminal window
mkdir -p ~/lab/b10/files/notes ~/lab/b10/files/archive ~/lab/b10/files/draft.txt
cd ~/lab/b10/files
touch notes/plan.txt notes/meeting.txt archive/plan.txt archive/data.csv .hidden.txt

draft.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.

Terminal window
find . -name 'plan.txt'
Captured output
./notes/plan.txt
./archive/plan.txt

find 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:

Terminal window
find notes -type f -name '*.txt'
Captured output
notes/plan.txt
notes/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?

Terminal window
find . -name '*.txt'
Run it, then compare with your guess
Captured output
./draft.txt
./notes/plan.txt
./notes/meeting.txt
./.hidden.txt
./archive/plan.txt

Yes. 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:

Terminal window
find . -type f -name '*.txt'
Captured output
./notes/plan.txt
./notes/meeting.txt
./.hidden.txt
./archive/plan.txt
Narrow the tree by name and type
learner@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.txtlisted-rw-r--r--
      • draft.txt/
      • .hidden.txtlistedhidden-rw-r--r--
      • notes/
        • meeting.txtlisted-rw-r--r--
        • plan.txtlisted-rw-r--r--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The exact name finds both plan.txt paths; the type-and-name search finds text files while excluding the directory draft.txt.
Steps (8)
  1. Type cd ~/lab/b10/files
  2. Press Enter
  3. Type find . -name 'plan.txt'
  4. Press Enter
  5. Output: ./notes/plan.txt ./archive/plan.txt
  6. Type find . -type f -name '*.txt'
  7. Press Enter
  8. 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:

Terminal window
find . -type d
Captured output
.
./draft.txt
./notes
./archive

The 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.

Bound the tree, then test entries The starting path bounds traversal. Name and type tests together select the entries printed. Start inside the practice tree Name matches the quoted pattern Type is a regular file Print paths passing both tests
Bound the tree, then test entries The starting path bounds traversal. Name and type tests together select the entries printed. Start inside the practice tree Name matches the quoted pattern Type is a regular file Print paths passing both tests
The starting path bounds traversal; the name and type tests together select which entries are printed.

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.

-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:

Terminal window
find . -path './archive/*.txt' -type f
Captured output
./archive/plan.txt

The 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.

Searching a missing starting directory.

Terminal window
find missing -name 'plan.txt'
Captured output
find: ‘missing’: No such file or directory

The 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.

Terminal window
find . -type f -name '*.pdf'
echo "$?"
Captured output
0

A 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.

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.

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
Terminal window
find archive -type f -name '*.csv'
Captured output
archive/data.csv

The result is the path to archive/data.csv. The suffix selects the name; -type f establishes the entry type independently.

Terminal window
cd ~/lab/b10
ls -AR files
Terminal window
rm -r ~/lab/b10/files

Rerun Before you start. If the directory is already absent, skip removal.

  • An explicit starting path bounds the tree find searches.
  • -name tests a final name; -path tests the path including the starting-path prefix.
  • -type f requires a regular file; -type d requires a directory.
  • Quote wildcard patterns so find receives them unchanged. A database search can be stale.

Next, Searching inside files selects lines from file contents.

  • 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).