Terminal GuideTerminal Guide

find Command Guide

The find command is one of the most versatile tools in Linux for locating files and directories. Learn how to search your filesystem efficiently with practical examples.

7 min readLast updated: January 19, 2025
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

find . -name "*.txt"Find by name in current dir
find /path -type fFind files in path
find /path -type dFind directories in path

By Name

-name "pattern"Case-sensitive name match
-iname "pattern"Case-insensitive match
-name "*.js"Wildcard pattern

By Size

-size +100MLarger than 100MB
-size -1kSmaller than 1KB
-emptyEmpty files/directories

By Time

-mtime -7Modified in last 7 days
-mtime +30Modified over 30 days ago
-mmin -60Modified in last 60 min

By Type

-type fRegular files
-type dDirectories
-type lSymbolic links

Actions

-exec cmd {} \;Execute command on each
-deleteDelete matched files
-printPrint pathname (default)

Downloadable Image Preview

Failed to generate preview

Basic Usage

The find command searches for files in a directory hierarchy. The basic syntax is straightforward: specify where to search and what to look for.

bash
find /path/to/search -name "filename"

If you omit the path, find will search in the current directory.

bash
# Search in current directory
find . -name "*.txt"

Searching by Name

The most common use case is searching for files by name. You can use wildcards and case-insensitive searches.

Exact name match

bash
find /home -name "config.json"
bash
find . -iname "readme.md"

Wildcard patterns

bash
# Find all JavaScript files
find ./src -name "*.js"

# Find files starting with "test"
find . -name "test*"

# Find files with specific pattern
find . -name "*config*.json"
Tip
Always quote wildcard patterns to prevent shell expansion. Use single or double quotes around patterns like "*.txt".

Searching by Type

You can filter results to show only files, directories, or other types.

bash
# Find only files
find . -type f -name "*.log"

# Find only directories
find . -type d -name "node_modules"

# Find symbolic links
find . -type l

Common Type Options

-type fRegular file
-type dDirectory
-type lSymbolic link

Searching by Size

Find files based on their size using the -size option.

bash
# Find files larger than 100MB
find . -size +100M

# Find files smaller than 1KB
find . -size -1k

# Find files exactly 50 bytes
find . -size 50c

# Find empty files
find . -type f -empty

Size Units

cBytes
kKilobytes
MMegabytes
GGigabytes

Searching by Time

Find files based on when they were modified, accessed, or created.

bash
# Modified in the last 7 days
find . -mtime -7

# Modified more than 30 days ago
find . -mtime +30

# Modified in the last 60 minutes
find . -mmin -60

# Accessed in the last 24 hours
find . -atime -1
Info
-mtime uses days, while -mmin uses minutes. The same applies to -atime/-amin (access time) and -ctime/-cmin (change time).

Executing Commands

One of find's most powerful features is the ability to execute commands on the files it finds.

Using -exec

bash
# Delete all .tmp files
find . -name "*.tmp" -exec rm {} \;

# Change permissions on all shell scripts
find . -name "*.sh" -exec chmod +x {} \;

# View contents of all found files
find . -name "*.log" -exec cat {} \;

Using -exec with confirmation

bash
# Ask before deleting each file
find . -name "*.bak" -exec rm -i {} \;

# Or use -ok for built-in confirmation
find . -name "*.bak" -ok rm {} \;
Warning
Be careful with -exec rm. Always test your find command without the -exec part first to see what files would be affected.

Combining Conditions

Use logical operators to combine multiple search conditions.

bash
# AND (implicit) - both conditions must match
find . -name "*.js" -size +10k

# OR - either condition matches
find . -name "*.js" -o -name "*.ts"

# NOT - exclude matches
find . -type f ! -name "*.log"

# Complex combinations with parentheses
find . \( -name "*.js" -o -name "*.ts" \) -mtime -7

Practical Examples

Clean up old log files

bash
find /var/log -name "*.log" -mtime +30 -delete

Find large files in home directory

bash
find ~ -type f -size +100M -exec ls -lh {} \;

Find and list all Git repositories

bash
find ~ -type d -name ".git" 2>/dev/null

Find files with specific permissions

bash
# Find world-writable files
find /var -perm -002 -type f

# Find setuid files
find / -perm -4000 -type f 2>/dev/null

Search and replace in multiple files

bash
find . -name "*.txt" -exec sed -i 's/old/new/g' {} \;

Performance Tips

Here are some tips to make your find commands faster and more efficient.

bash
# Limit search depth
find . -maxdepth 2 -name "*.js"

# Skip certain directories
find . -name "node_modules" -prune -o -name "*.js" -print

# Use -quit to stop after first match
find . -name "config.json" -quit
Tip
For simple filename searches, consider using locate command which uses a pre-built database and is much faster. Install it with apt install mlocate on Debian/Ubuntu.

Summary

The find command is incredibly powerful for locating files based on various criteria. Key takeaways:

  • Use -name for name patterns, -iname for case-insensitive
  • Filter by type with -type f (files) or -type d (directories)
  • Search by size with -size and time with -mtime
  • Execute commands on results with -exec
  • Combine conditions with -a (AND), -o (OR), and ! (NOT)

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles