Loading...

Linux File Searching: find, locate, which, whereis, and grep Recursively

Back to blog
LinuxFile ManagementSearchSystem Administration

Linux File Searching: find, locate, which, whereis, and grep Recursively

Master Linux file searching commands including find with multiple criteria, locate for fast searches, which for command location, and recursive file discovery.

9 min read

Linux File Searching: find, locate, which, whereis, and grep Recursively

Finding files efficiently is a critical skill for Linux users and administrators. Different tools serve different purposes, from quick searches to complex queries.

find - Powerful File Search

Search files with extensive options.

# Find by name
find /home -name "*.txt"
 
# Case-insensitive search
find /home -iname "*.TXT"
 
# Find in current directory
find . -name "file.txt"
 
# Find any file type
find / -name "*config*" 2>/dev/null
 
# Exclude directories
find /home -name "*.txt" ! -path "*/.*"
 
# Search by file type
find /home -type f          # Regular files
find /home -type d          # Directories
find /home -type l          # Symbolic links
find /home -type s          # Socket
find /home -type p          # Pipe
find /home -type c          # Character device
find /home -type b          # Block device
 
# By size
find /home -size +1M        # Larger than 1 MB
find /home -size -1M        # Smaller than 1 MB
find /home -size 1M         # Exactly 1 MB
find /home -size +10G       # Larger than 10 GB
 
# By modification time
find /home -mtime -1        # Modified in last 24 hours
find /home -mtime +30       # Modified more than 30 days ago
find /home -mtime 7         # Modified exactly 7 days ago
find /home -mmin -30        # Modified within 30 minutes
 
# By access time
find /home -atime -1        # Accessed in last day
find /home -amin -60        # Accessed within 1 hour
 
# By change time
find /home -ctime -1        # Changed in last day
 
# By permissions
find /home -perm 644        # Exact permissions
find /home -perm -644       # At least 644
find /home -perm /644       # At least one of rwxrw-r--
 
# By ownership
find /home -user john       # Files owned by john
find /home -group developers # Files owned by developers
find /home -uid 1000        # Files with UID 1000
 
# By ownership and type
find /home -user john -type f
 
# Readable by owner
find /home -perm /u=r
 
# Executable by anyone
find /home -perm /a=x
 
# Empty files/directories
find /home -empty
 
# Symbolic links
find / -type l              # Find all symlinks
find / -type l -o -type d   # Symlinks or directories
 
# Multiple criteria (AND)
find /home -type f -name "*.txt" -mtime -7
 
# Multiple criteria (OR)
find /home \( -name "*.txt" -o -name "*.md" \)
 
# Limit depth
find /home -maxdepth 2 -name "*.txt"
find /home -mindepth 2 -name "*.txt"
 
# Execute command on results
find /home -name "*.txt" -exec wc -l {} \;
find /home -name "*.txt" -exec rm {} \;
find /home -name "*.txt" -delete
 
# Execute with prompt
find /home -name "*.txt" -ok rm {} \;
# Prompts before each action
 
# Print path for each result
find /home -name "*.txt" -print
 
# Print with null delimiter (safe for xargs)
find /home -name "*.txt" -print0
 
# Pipe to xargs
find /home -name "*.txt" -print0 | xargs -0 wc -l
 
# Show find statistics
find /home -name "*.txt" -printf "Found: %p\n"

locate - Fast File Search

Search using database (very fast).

# Basic search
locate filename
 
# Find all files with pattern
locate "*.txt"
 
# Case-insensitive search
locate -i filename
 
# Count results
locate -c filename
 
# Show only existing files
locate -e filename
 
# Regex search
locate -r 'pattern'
 
# Search from specific directory
locate -d /var/lib/mlocate/locate.db filename
 
# Update database
sudo updatedb
 
# Create database for specific path
sudo updatedb -l 0 -o /tmp/locate.db -U /home
 
# Show database info
locate -S
# Database /var/lib/mlocate/locate.db:
# 	 1,234,567 directories
# 	 5,678,901 files
# 	 123 MB
 
# Faster than find for simple searches
# Updated daily by cron

which - Locate Command

Find executable location.

# Show command location
which python3
# /usr/bin/python3
 
# Check if command exists
which npm
# /usr/local/bin/npm
 
# Show all matches
which -a python
# /usr/bin/python
# /usr/local/bin/python
 
# Check multiple commands
which ls pwd cd
 
# Verbose output
which -v python
 
# Check PATH
echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

whereis - Locate Binary, Source, and Manual

# Find command and documentation
whereis python
# python: /usr/bin/python3.8 /usr/bin/python3.8-config /usr/lib/python3.8 ...
 
# Binary only
whereis -b python
 
# Source only
whereis -s python
 
# Manual only
whereis -m python
 
# Unusual entries
whereis -u python

type - Command Type

Show command type and location.

# Check if builtin or external
type cd
# cd is a shell builtin
 
type ls
# ls is aliased to `ls --color=auto'
 
type python
# python is /usr/bin/python
 
# Show all information
type -a ls
# Shows all matches including aliases
 
# Show path only
type -P python
# /usr/bin/python

Grep Recursive Searching

Search within files recursively.

# Recursive grep
grep -r "pattern" /home/
 
# Case-insensitive recursive
grep -ri "pattern" /home/
 
# Show file names and line numbers
grep -rn "error" /var/log/
 
# Search in specific file types
grep -r --include="*.py" "def main" /home/projects/
 
# Exclude file types
grep -r --exclude="*.log" "pattern" /home/
 
# Exclude directories
grep -r --exclude-dir=.git "pattern" /home/
 
# Show only matching files
grep -rl "pattern" /home/
 
# Show with context
grep -rn -C 2 "error" /var/log/
 
# Count matching files
grep -rl "pattern" /home/ | wc -l
 
# Invert match
grep -rv "debug" /home/projects/
 
# Word boundary
grep -rw "error" /var/log/
 
# Count matches per file
grep -rc "pattern" /home/

Specialized Search Tools

fd - Modern Find Alternative

Fast, user-friendly find replacement.

# Install fd
sudo apt install fd-find
 
# Basic search
fd "pattern"
 
# Search specific type
fd -t f "*.txt"         # Files
fd -t d "documents"     # Directories
fd -t l "symlinks"      # Symlinks
 
# Case-sensitive
fd -s "Pattern"
 
# Hidden files
fd -H "pattern"
 
# Full path search
fd -p "\.config/.*txt"
 
# Extension search
fd -e txt               # All .txt files
fd -e rs -e txt         # Multiple extensions
 
# Execute command
fd -e log -x wc -l {}   # Count lines in log files
 
# Limit depth
fd -d 2 "pattern"
 
# Maximum depth
fd -d 3 "*.txt"

rg - ripgrep (Fast Grep Alternative)

Ultra-fast grep replacement.

# Install ripgrep
sudo apt install ripgrep
 
# Basic search
rg "pattern"
 
# Case-insensitive
rg -i "pattern"
 
# Word boundary
rg -w "pattern"
 
# File type
rg -t py "pattern"      # Python files
rg -t sh "pattern"      # Shell files
 
# Show only file names
rg -l "pattern"
 
# Count matches
rg -c "pattern"
 
# Show context
rg -C 3 "pattern"
 
# Exclude files
rg --exclude "*.log" "pattern"
 
# Search specific path
rg "pattern" /home/
 
# Statistics
rg --stats "pattern"
 
# Much faster than grep for large searches

Practical File Search Examples

Find Large Files

# Find largest files
find / -type f -exec ls -lh {} \; | sort -k5 -h | tail -20
 
# Find files larger than 1GB
find / -type f -size +1G
 
# Files larger than 100MB
find / -type f -size +100M | head -20

Find Recently Modified Files

# Files modified today
find / -type f -mtime -1
 
# Files modified in last hour
find / -type f -mmin -60
 
# Recently created files
find / -type f -mtime -1 | sort -t/ -k10

Find Duplicate Files

# Find files with same name
find /home -type f -name "*.txt" | sort | uniq -d
 
# Find identical files (by size and hash)
find /home -type f -exec md5sum {} \; | sort | uniq -d -w 32

Find Configuration Files

# Find config files
find /etc -type f -name "*.conf"
 
# Find all configs
find / -type f \( -name "*.conf" -o -name "*.config" -o -name "*.cfg" \)
 
# Find in user homes
find /home -type f -name ".config" -o -name ".bashrc"

Batch Operations

# Delete files older than 30 days
find /tmp -type f -mtime +30 -delete
 
# Change permissions on files
find /home -type f -name "*.sh" -exec chmod +x {} \;
 
# Archive old logs
find /var/log -type f -name "*.log" -mtime +7 -exec gzip {} \;
 
# Count files by type
find /home -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn

Performance Optimization

# Faster searches
# 1. Limit search depth
find /home -maxdepth 3 -name "*.txt"
 
# 2. Search specific directories
find /home/john -name "*.txt"  # Not /
 
# 3. Use locate for initial search
locate "*.txt"
 
# 4. Use -print0 with xargs
find /home -name "*.txt" -print0 | xargs -0 grep "pattern"
 
# 5. Exclude unnecessary directories
find / -name "*.txt" \
  -not -path "*/\.*" \
  -not -path "*/node_modules/*" \
  -not -path "*/\.git/*"
 
# 6. Use fd for better performance
fd "pattern"

Comparison of Tools

Tool Speed Features Use Case
find Slow Very flexible Complex queries
locate Very Fast Basic search Quick filename search
which Instant Command location Find executables
grep -r Medium Pattern matching Search file contents
rg Very Fast Rich features Content search in code
fd Fast User-friendly Modern alternative to find

Best Practices

  1. Know Your Tool - Each tool excels at different tasks
  2. Use Locate First - For simple filename searches
  3. Limit Scope - Search specific directories when possible
  4. Exclude Unnecessary - Avoid .git, node_modules, etc.
  5. Update Database - Keep locate database fresh
  6. Safe Execution - Use -x with find for file operations
  7. Test First - Always test complex find commands
  8. Use Appropriate Flags - Know what each flag does
  9. Chain Commands - Combine tools for complex needs
  10. Performance - Consider search time for large directories

Summary

File searching is essential for Linux administration:

  • find offers comprehensive search capabilities
  • locate provides fast filename searches
  • which finds executables quickly
  • grep -r searches file contents recursively
  • Modern alternatives like fd and rg are faster
  • Proper tool selection improves efficiency significantly

Master these tools to work efficiently with Linux filesystems.