Searching for files, cleaning logs, or executing automated batch scripts in the Linux terminal using the find command only to encounter the error: “find: paths must precede expression” or “find: possible unquoted pattern after predicate” abruptly halts command execution. In Linux and UNIX-like systems, the POSIX find utility enforces a strict grammatical syntax: find [path...] [expression]. The utility requires all directory search paths to be specified first, followed by filtering predicates (such as -name, -type, or -mtime). When a user inadvertently misplaces flags or—more commonly—fails to quote wildcard characters (such as * or ?), the shell expands the pattern before find ever executes, corrupting the argument list. This guide breaks down why this syntax error occurs and provides clear examples to fix it.
What Causes ‘paths must precede expression’ in find?
The Linux shell (Bash, Zsh) processes command-line arguments in sequential phases before handing them to executables:
- Unquoted Wildcard Expansion (Globbing): When executing
find . -name *.log, Bash expands*.loginto all matching filenames in the current working directory (e.g.,access.log error.log system.log). The resulting command executed becomesfind . -name access.log error.log system.log, causingfindto encounter unexpected positional tokens. - Flag and Predicate Positional Ordering: Placing search paths after operational flags (for example, writing
find -type f /var/loginstead offind /var/log -type f). - Missing Search Path Arguments: Omitting the search root entirely when executing custom scripts, causing
findto interpret the first flag as an invalid path. - Improperly Escaped Logical Operators: Forgetting to escape grouping parentheses
( ... )or logical OR switches (-o).
Method 1: Quote All Wildcard Patterns (Single or Double Quotes)
The overwhelming majority of “paths must precede expression” errors are caused by premature shell globbing. Wrapping search strings in quotes prevents Bash from expanding wildcards before find receives them.
Incorrect Syntax (Triggers Error):
# If multiple .txt files exist in current directory, this fails:
find . -name *.txt
Correct Syntax (Works Reliably):
# Quoting preserves the literal asterisk for find to evaluate internally:
find . -name "*.txt"
# Alternatively, single quotes or backslash escaping:
find . -name '*.txt'
find . -name *.txt
Method 2: Correct Flag and Path Positional Ordering
The find utility requires all search root directories to appear immediately after the find binary name, before any operational flags.
Incorrect Syntax:
# Incorrect: Flags placed before the directory path
find -type f -name "*.conf" /etc/
Correct Syntax:
# Correct: Directory path placed immediately after 'find'
find /etc/ -type f -name "*.conf"
# If searching the current working directory, specify '.' explicitly:
find . -type f -name "*.conf"
Method 3: Handle Argument Spaces and Complex Actions Properly
When executing commands on discovered files using the -exec action, improper terminator syntax causes argument parsing confusion.
Correct Syntax for -exec:
Ensure there is a space before the closing semicolon, and escape the semicolon so Bash does not treat it as a command delimiter:
# Correct format with escaped semicolon:
find /var/log -type f -name "*.log" -exec rm -f {} ;
# Or use the high-performance plus (+) batch terminator:
find /var/log -type f -name "*.log" -exec rm -f {} +
Method 4: Escape Grouping Parentheses for Logical Expressions
When combining multiple predicates with logical AND/OR operations, parentheses must be escaped with backslashes so the shell does not mistake them for subshell execution.
Incorrect Syntax:
# Triggers syntax error:
find . ( -name "*.jpg" -o -name "*.png" ) -type f
Correct Syntax:
# Correct: Escaped parentheses with spaces around them
find . ( -name "*.jpg" -o -name "*.png" ) -type f
Method 5: Handle Hidden Files Safely in Shell Scripts
When writing automated Bash scripts, enable or disable nullglob options if your scripts rely on variable expansion inside find queries.
# In automated scripts, always wrap variable references in double quotes:
SEARCH_DIR="/var/www/html"
FILE_PATTERN="*.php"
find "$SEARCH_DIR" -type f -name "$FILE_PATTERN"
find Command Syntax Reference Matrix
| Faulty Command | Syntax Error | Corrected Command |
|---|---|---|
find . -name *.tar.gz |
Unquoted shell glob expansion | find . -name "*.tar.gz" |
find -type d /home |
Path placed after predicate flag | find /home -type d |
find . -name *.sh -exec chmod +x {}; |
Unescaped semicolon terminator | find . -name "*.sh" -exec chmod +x {} ; |
find /tmp -name *.tmp -delete |
Multiple files expand before flag | find /tmp -name "*.tmp" -delete |
Always quoting file pattern strings and placing search paths immediately following find guarantees predictable, error-free file searches across all Linux environments.