Search Files in Linux: Find Command Guide
To search for files in Linux, you use the find command, which locates files and directories based on name, type, size, permissions, and other attributes. This guide explains the essential search commands, their syntax, and practical examples for everyday use.
Why Searching Files in Linux Differs from Windows or macOS
Linux has no single built-in graphical search tool that indexes everything like Spotlight on macOS or the Windows Search index. Instead, the command line provides powerful, scriptable tools that search the filesystem directly. The two most common are find and locate. find searches in real time by traversing directories, while locate uses a pre-built database for near-instant results. Understanding their differences helps you choose the right tool for the task.
The find Command: Syntax and Basic Usage
The basic syntax is:
find [starting directory] [expression]
For example, to find a file named report.pdf in your home directory:
find ~ -name "report.pdf"
The -name option matches the exact filename (case-sensitive). Use -iname for case-insensitive matching. To search the entire filesystem, start at / (you may need sudo to avoid permission errors).
Searching by Name, Type, and Size
You can combine criteria to narrow results:
- By type:
find / -type ffinds files,-type dfinds directories,-type lfinds symbolic links. - By size:
find / -size +100Mfinds files larger than 100 MB. Suffixes includec(bytes),k,M,G. - By modification time:
find / -mtime -7finds files modified in the last 7 days,+7for older than 7 days. - By permissions:
find / -perm 644finds files with exactly those permissions.
You can combine tests with -and (default), -or, and -not. For example, to find all empty files in /tmp:
find /tmp -type f -empty
Using find with Actions: Executing Commands on Results
find can run commands on each match using -exec or -delete. For instance, to delete all .tmp files in the current directory:
find . -name "*.tmp" -delete
To list details of each match:
find . -name "*.log" -exec ls -lh {} \;
The {} is replaced by the filename, and the semicolon must be escaped (\;) to avoid shell interpretation. Alternatively, use -exec ... + to batch multiple files into one command, which is more efficient.
The locate Command: Fast but Requires a Database
locate searches a database of filenames that is usually updated daily via updatedb. To use it:
locate report.pdf
It returns results instantly, but the database may be stale. To update it manually, run sudo updatedb. If locate is not installed, install it with your package manager (e.g., sudo apt install mlocate on Debian/Ubuntu). Because it only matches filenames (not contents), it is best for finding files by name when speed matters.
Advanced Searches: Content, Regular Expressions, and Logical Operators
To search inside file contents, use grep. Combined with find, you can search all files of a certain type for a string:
find . -name "*.txt" -exec grep -l "error" {} \;
Alternatively, use grep -r to search recursively:
grep -r "error" /var/log
For advanced filename matching, find supports regular expressions with -regex (the full path is matched, not just the basename). For example, to find files ending in .txt or .md:
find . -regex ".*\.\(txt\|md\)"
Logical operators like ! -name "*.tmp" exclude files, and parentheses group expressions (escape them for the shell).
Common Pitfalls and How to Avoid Them
- Permission denied errors: When searching from
/, you will see many errors. Redirect stderr to/dev/null:find / -name "foo" 2>/dev/null. - Case sensitivity: Use
-inameif you are unsure of the case. - Escaping wildcards: If you quote the pattern, the shell does not expand it:
find . -name "*.txt"works;find . -name *.txtmay fail if multiple matches exist in the current directory. - Deleting files accidentally: Always test with
-printfirst (or use-exec echo) before using-delete. - Using
locateafter creating a file: The database may not be updated yet; runsudo updatedbor usefind.
Frequently Asked Questions
How do I search for a file by name in the current directory?
Use find . -name "filename". The dot represents the current directory. To include subdirectories, this already does that recursively. To search only the current directory (not subdirectories), add -maxdepth 1.
What is the difference between find and grep?
find searches for files and directories by attributes like name, size, and modification time. grep searches inside file contents for text patterns. They are often used together: find to get a list of files, then grep to filter by content.
How do I search for files modified in the last hour?
Use find . -mmin -60. The -mmin option takes minutes. For days, use -mtime -1 for the last 24 hours.
Can I search for files by size range?
Yes, combine two size tests with -and (or just put them together). For example, find . -size +10M -size -50M finds files between 10 MB and 50 MB.
Why does locate not find a file that exists?
The locate database is updated periodically (usually daily). If the file was created after the last update, locate won’t see it. Run sudo updatedb to refresh the database manually, or use find for real-time results.
How do I search for files with a specific extension?
Use a wildcard with -name: find . -name "*.jpg". For multiple extensions, use -o (or) with parentheses: find . \( -name "*.jpg" -o -name "*.png" \).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.