Skip to main content

Name and Type Filters

The two most frequently used predicates in find are -name (and its variants) and -type. Filtering by these attributes first is crucial for performance, as they are "cheap" operations that prevent find from performing more expensive calculations (like time or size) on irrelevant files.

1. Type Filtering (-type)

Everything in a Unix filesystem is a file—even directories and hardware devices. The -type flag filters results based on the object's inode type.

Type FlagDescriptionCommon Use Case
fRegular FileFinding documents, scripts, binaries.
dDirectoryFinding folders to prune or calculate size for.
lSymbolic LinkAuditing shortcuts or broken links.
sSocketFinding IPC endpoints (e.g., mysql.sock).
cCharacter DeviceFinding unbuffered devices (e.g., /dev/tty).
bBlock DeviceFinding buffered storage devices (e.g., /dev/sda).

Example:

# Find only directories in /var/log
find /var/log -type d
tip

Always use -type! Even if you are searching by name, specifying -type f or -type d makes your intent clear and slightly optimizes the search.

2. Name Filtering (-name / -iname)

The -name predicate filters based on the basename of the file (the final component of the path).

Exact Match

If you know the exact name of the file:

find /etc -type f -name "passwd"

Glob Patterns (Wildcards)

You can use standard shell globbing to match patterns. Crucially, you must quote the pattern. If you do not quote it, your shell will expand the wildcard before passing it to find, leading to syntax errors or unexpected behavior.

# Correct: Quotes prevent shell expansion
find /var/www -type f -name "*.php"

# Find any file starting with 'config'
find /etc -type f -name "config*"

# Find files with exactly 3 characters in the extension
find /tmp -type f -name "*.???"

Case-Insensitive Matching (-iname)

To ignore case sensitivity, use -iname. This is a GNU extension but widely supported on modern Linux distributions.

# Matches config.xml, Config.XML, CONFIG.xml, etc.
find /var/opt -type f -iname "config.xml"

3. Path Filtering (-path / -ipath)

While -name only evaluates the basename of the file, -path evaluates the entire path string relative to the starting point.

This is highly useful when you want to find files inside specific directory structures regardless of their exact name.

# Find any .log file, but ONLY if it's inside an 'archive' directory
find /var/log -type f -path "*/archive/*.log"

Note: The -path pattern must match the entire string find generates. If the starting path is /var/log, the string evaluated might be /var/log/nginx/archive/error.log. The pattern */archive/*.log successfully matches this entire string.