VMware ESXi and vSphere Cluster Management
Search for Files in Linux Using the find Command
Learn how to use Linux find to search directory trees by name, size, ownership, permissions, file type, and combined criteria.
What the find Command Does
find is a Linux command that recursively searches a directory tree for files and directories. It examines the filesystem when you run it, rather than consulting a prebuilt list of filenames.
This live search makes find flexible: it can test names, sizes, owners, groups, permissions, and object types in one command. The trade-off is that a large search can take time because find visits directories and entries as it runs.
The locate command is often faster because it usually consults a maintained filename database. However, that database may not include recently created, moved, or deleted files until its index is updated. Use find when you need current filesystem results or detailed filtering.
Basic Syntax
find PATH EXPRESSIONPATH is the directory where traversal begins. An absolute path such as /home starts at that exact location. A relative path such as projects is interpreted from the current directory. The special path . means the current directory.
An expression is a test, logical operator, or action. Tests decide whether each filesystem entry matches. This lesson focuses on search tests; actions can be added later to perform operations on matching entries.
find . -name "report.txt"This command starts in the current directory and searches every nested directory for an entry named exactly report.txt.
Searching by Name
Case-Sensitive Name Matching with -name
-name matches the name of a file or directory using a case-sensitive shell-style pattern. It does not require the complete name to be literal; wildcard characters can represent variable parts.
find . -name "so*"
find . -name "*.log"
find . -name "*backup*""so*"matches names beginning withso."*.log"matches names ending in.log."*backup*"matches names containingbackup.
A wildcard is a pattern character such as * that can match a variable portion of a name. Quote wildcard patterns so the shell passes the pattern to find unchanged. Without quotes, the shell may expand *.log to matching names in the current directory before find runs, producing unintended behavior.
find . -name "so*"This searches the current directory tree for all files and directories whose names begin with so.
Case-Insensitive Matching with -iname
-name treats uppercase and lowercase letters as different. Use -iname when capitalization should not matter.
find . -type d -iname "projects*"This finds directories whose names begin with projects, including names such as Projects or PROJECTS-old.
Restricting Results to Files or Directories
By default, find can return both regular files and directories. Use -type when you need a particular kind of filesystem object.
-type fmatches regular files.-type dmatches directories.
find . -type f -name "*.conf"
find . -type d -name "cache*"The first command searches only regular files ending in .conf. The second searches only directories whose names begin with cache.
Searching by File Size
The -size test filters entries by size. Add a unit suffix to make the intended unit clear:
| Syntax element | Meaning | Example |
|---|---|---|
c | Bytes | -size 300c |
k | Kilobytes | -size +100k |
M | Megabytes | -size -10M |
G | Gigabytes | -size +1G |
+ | Greater than the specified size | -size +300c |
- | Less than the specified size | -size -10M |
| No prefix | Exact size unit used by the test | -size 300c |
For example, +300c means larger than 300 bytes, while -10M means smaller than 10 megabytes. When using larger units such as kilobytes or megabytes, remember that find applies its unit-based size rules and may round values to the selected unit. Use the byte unit when you need a precise byte threshold.
find . -type f -size +300cThis finds regular files larger than 300 bytes. A size search may appear to miss an expected file if the wrong suffix or comparison prefix was used, so check whether the requirement is bytes, kilobytes, megabytes, or gigabytes and whether the comparison should be exact, greater than, or less than.
Searching by Ownership
Owner, Numeric User ID, and Group
Linux records an owner and a group for filesystem entries. Use -user with a username, -uid with a numeric user ID, or -group with a group name.
| Expression | Purpose | Example |
|---|---|---|
-name | Case-sensitive name pattern | find . -name "*.txt" |
-iname | Case-insensitive name pattern | find . -iname "readme*" |
-size | Size comparison | find . -size +1M |
-user | Owner username | find /home -user username |
-uid | Numeric owner ID | find /srv -uid 1001 |
-group | Group ownership | find . -group developers |
-perm | Permission-bit matching | find . -perm 775 |
-type | Object type | find . -type f |
find /path/to/search -user username
find /path/to/search -uid 1001
find /path/to/search -group developersReplace username, 1001, and developers with values from your system. Numeric IDs are useful when accounts are identified by IDs or when a username is unavailable.
Searching by Permissions
Linux permissions control read, write, and execute access for three classes: the owner, the group, and others. Symbolic notation writes these classes as u, g, and o. For example, rwxr-xr-x grants the owner read, write, and execute access, while the group and others have read and execute access.
Octal permissions use one digit for each class. The values are 4 for read, 2 for write, and 1 for execute; values are added within each class. Therefore, 775 means:
7for the owner: read, write, and execute.7for the group: read, write, and execute.5for others: read and execute.
find /path/to/search -perm 775With a mode written without a prefix, such as -perm 775, the usual meaning is an exact permission-mode match: the relevant permission bits must equal 775. GNU find also supports permission-bit matching:
-perm -775matches entries with every bit in775enabled, even if additional permission bits are enabled.-perm /775matches entries with at least one bit from775enabled.
Permission syntax and supported forms can vary slightly between Unix-like systems, so consult the local find manual when portability matters.
Combining Search Expressions
Multiple compatible tests placed next to each other have implicit AND behavior. An entry must satisfy every test to be returned.
find . -name "t*" -perm 775This finds entries whose names begin with t and whose permission mode is exactly 775.
You can make the logic explicit with -and, choose alternatives with -or, and negate a test with !.
find . -type f -and -name "*.log"
find . -type f \( -name "*.log" -or -name "*.txt" \)
find . ! -name "*.tmp"Parentheses group alternatives, but they must be escaped or quoted so the shell does not interpret them. The second example matches regular files ending in either .log or .txt. The third excludes names ending in .tmp.
Safe Command-Line Usage
Quote Patterns and Paths
Quote wildcard patterns such as "*.log", "so*", and "*backup*". Also quote a starting path containing spaces:
find "My Documents" -type f -name "*.txt"Single quotes and double quotes both protect spaces and wildcard characters in ordinary cases. For example, find . -name '*.log' is also valid.
Choose a Narrow Starting Path
Begin with the narrowest suitable directory, such as a project directory or your home directory, rather than immediately searching from /. A broad search walks many more entries, can be slow, and may encounter directories your account cannot read.
find /home/username/projects -type f -name "*.py"Permission-denied messages mean the current user cannot read one or more directories in the traversal. Search accessible paths, adjust permissions when appropriate, or use elevated privileges only when there is a clear administrative reason. Do not use elevated access merely to avoid understanding which paths are readable.
Troubleshooting Common Problems
- Unexpected wildcard results: quote the pattern, such as
find . -name "*.log", so the shell does not expand it first. - Directories appear in a file search: add
-type f. Use-type dwhen you want directories. - No result because of capitalization: replace
-namewith-iname. - The search is slow: use a more specific starting path and add restrictive tests such as
-type fand-name. - Permission-denied messages: limit the search to accessible paths or address access deliberately.
- Unexpected size matches: verify the unit suffix and comparison prefix. Use
cfor bytes and add+or-for thresholds.
Quick Reference
find PATH EXPRESSION
find . -name "so*"
find . -type f -size +300c
find /path/to/search -user username
find /path/to/search -perm 775
find . -name "t*" -perm 775
find . -type d -iname "projects*"