Skip to content
TaeyoungKim.dev

Linux find -name '*.log': Why nested files disappear without quotes

LinuxWritten 3 min readTaeyoungKim
LinkedInX

archive/worker.log exists, yet your find command returns only app.log. Before blaming find, check whether the shell changed *.log before passing the argument to find.

Does find -name search filenames or file contents?

find . -name '*.log' walks down from the current directory (.) and matches names against *.log. It does not read the files to look for the word error. grep answers the separate question of which lines contain text. Use find for “Which file?” and grep for “Which line in that file?”

The * in a -name pattern can match any prefix of the name. Without quotes, however, the shell gets a chance to expand it first. A tiny directory demonstrates the difference. GNU Findutils documents why patterns containing wildcards should be quoted.

How does an unquoted *.log miss a nested file?

Create two logs in a temporary directory so the demonstration does not change existing files. One is in the current directory; one is under archive.

bash
demo_dir=$(mktemp -d)
mkdir -p "$demo_dir/archive"
touch "$demo_dir/app.log" "$demo_dir/archive/worker.log"
cd "$demo_dir"
find . -type f -name *.log | sort
text
./app.log

Because the current directory contains one matching app.log, the shell expands *.log to that filename. The condition passed to find is effectively -name app.log. find still visits archive/worker.log, but its name does not match.

Quote the pattern to pass it unchanged to find:

bash
find . -type f -name '*.log' | sort
text
./app.log
./archive/worker.log

Quoting decides which program interprets the pattern. If the current directory contains several matching files or none, the unquoted command can fail differently depending on the shell configuration. Do not depend on an accidental match.

The unquoted example hands app.log to find. Quoting lets find match both the current and nested log filenames.

What does -type f add to -name?

Now create a directory whose name also ends in .log:

bash
mkdir "$demo_dir/old.log"
find . -name '*.log' | sort
text
./app.log
./archive/worker.log
./old.log

-name alone matches both files and directories. Add -type f when you want regular files only:

bash
find . -type f -name '*.log' | sort
text
./app.log
./archive/worker.log

For directories, use -type d. Name and file type are separate conditions. Before piping a file list into another command, decide which types should be included.

If find returns nothing, what should you check?

Start with pwd. The dot in find . means the current directory, not the whole system. Next check the quotes around the pattern and whether you requested regular files or directories.

No matching paths does not by itself mean the command failed. If the directory has no .txt files, this search prints no matches yet exits normally:

bash
find . -type f -name '*.txt'
echo $?
text
0

Automation must check for an actual match rather than treating exit status 0 as proof that a file was found. Choose the application's working directory before widening a search into unrelated or inaccessible directories.

Key takeaways

find searches path names; grep searches file contents. If nested logs disappear, quote the pattern as in find . -type f -name '*.log' so the shell cannot expand it first. Then verify the start directory, file type, and whether any paths actually matched.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Linux#find command#find -name#Filename search#Shell globbing

Read next