Linux find Command Examples

The find command recursively searches directory trees for files and directories matching your criteria. It supports filtering by name, type, size, modification time, and permissions. Combined with -exec or xargs, find can apply any command to matched files in bulk, making it a powerful automation building block.

LinuxfindCLIFile System
Find by Name & Type
Find files by name pattern
find . -name "*.log"
Searches recursively from the current directory for files matching the glob pattern. The dot . is the starting point — use a specific path like /var/log to search from a different location.
Case-insensitive name search
find . -iname "readme*"
-iname matches regardless of letter case, finding README.md, readme.txt, Readme.html, and all variants simultaneously. Useful when working with codebases that mix naming conventions.
Find only directories
find . -type d -name "node_modules"
Use -type d to restrict results to directories only, -type f for regular files, and -type l for symlinks. This finds all node_modules directories — useful before packaging or disk cleanup.
Find files matching either of two extensions
find . \( -name "*.py" -o -name "*.js" \)
Use -o for logical OR between conditions. Parentheses must be escaped with backslashes in the shell. This finds all Python and JavaScript files in the directory tree simultaneously.
Find by Attributes
Find files larger than a given size
find . -size +10M
Filter by file size using -size. The + prefix means "greater than". Units: k (KB), M (MB), G (GB). Use - for "less than". Great for finding disk-space hogs before cleanup.
Find files modified in the last N days
find . -mtime -7
-mtime -7 matches files modified within the last 7 days. Use +7 for files older than 7 days. Use -mmin -60 for files modified in the last 60 minutes.
Find empty files
find . -type f -empty
The -empty flag matches files with zero bytes. Combine with -type f to exclude empty directories. Useful for cleaning up placeholder files, failed downloads, or empty log files.
Execute Actions
Find and delete matching files
find . -name "*.tmp" -delete
The -delete action removes matched files. Always run find without -delete first to preview what will be removed. This is faster and safer than piping to rm via xargs for straightforward deletions.
Execute a command on each result
find . -name "*.sh" -exec chmod +x {} \;
The -exec action runs a command for each match. {} is replaced with the filename; \; terminates the expression. This example makes all shell scripts in the tree executable.
Find files containing a specific string
find . -name "*.py" -exec grep -l "TODO" {} \;
Combines find with grep to search inside file contents. The -l flag on grep prints only filenames with matches. Finds all Python files containing "TODO" — useful for code audits without a full IDE search.

How to Use

  1. Always test find without action flags first to preview what will be matched.
  2. Use -type f or -type d to limit results to files or directories.
  3. Add attribute filters like -size, -mtime, or -perm to refine results.
  4. Use -exec command {} \; to process each result with any command.
  5. Add 2>/dev/null to suppress "Permission denied" errors in restricted directories.

Frequently Asked Questions

Why does find show "Permission denied" errors?

find traverses all directories, including those you don't have permission to read. Suppress these errors by redirecting stderr: find . -name "*.log" 2>/dev/null. Alternatively, run with sudo when searching system directories.

What is the difference between -exec and xargs?

-exec runs a new process for each matched file, which is slower for large result sets. xargs batches multiple filenames into fewer invocations. Use find . -name "*.txt" -print0 | xargs -0 grep "pattern" for better performance with filenames that may contain spaces.

How do I limit the search depth?

Use -maxdepth N to limit recursion depth. For example, find . -maxdepth 2 -name "*.log" only searches the current directory and its immediate subdirectories. Use -mindepth to skip top-level results.

Related Tools