Common Errors
When find fails, it usually outputs cryptic syntax errors or floods the screen with permission denied warnings. Here is how to diagnose and fix the most common issues.
1. "paths must precede expression"
The Error:
find: paths must precede expression: `*.txt'
The Cause:
You failed to quote a glob pattern, and your shell expanded it before passing it to find. If the current directory contains a.txt and b.txt, the shell turns find . -name *.txt into find . -name a.txt b.txt. find sees b.txt as a second path, but it appears after the -name expression, violating the syntax rules.
The Fix: Always quote your search patterns.
find . -name "*.txt"
2. "Permission denied" Floods
The Error:
When searching from the root directory / as a non-root user, find outputs hundreds of lines of:
find: ‘/etc/ssl/private’: Permission denied
find: ‘/root’: Permission denied
This buries the actual results you are looking for.
The Cause:
find attempts to read the directory contents to traverse it. If you lack read permissions on a directory, it throws an error to stderr.
The Fix:
Redirect standard error (file descriptor 2) to /dev/null.
find / -name "my_document.txt" 2>/dev/null
3. "missing argument to -exec"
The Error:
find: missing argument to `-exec'
The Cause:
You forgot to terminate the -exec statement, or you failed to escape the terminating semicolon so the shell swallowed it.
The Fix:
Ensure the -exec action ends with a backslash and a semicolon \;, or a plus sign +. Note that there MUST be a space before the \;.
# Wrong
find . -type f -exec ls -l{} \;
find . -type f -exec ls -l {} \ ;
# Correct
find . -type f -exec ls -l {} \;
4. Find Ignores My Alias or Shell Function
The Problem:
You try to run find . -exec my_alias {} \; and it fails with "command not found".
The Cause:
The -exec action does not execute the command in your current shell environment. It spawns a fresh sub-shell or invokes the binary directly. Aliases and shell functions do not exist in this context.
The Fix: You must either:
- Call a physical script file instead of an alias.
- Explicitly invoke bash and pass the function (complex and error-prone).
- Pipe
findto awhile readloop inside your current shell.
# Executing an alias/function via while loop in the current shell context
find . -type f -print0 | while IFS= read -r -d '' file; do
my_alias "$file"
done