Skip to main content

Chapter 3.6 - File Search

Learning Objectives​

By the end of this chapter, you will be able to:

  • Use find effectively
  • Use locate for quick search
  • Search by content with grep
  • Combine search tools

Basic Syntax​

find [path] [criteria] [actions]

Search by Name​

# By exact name
find /home -name "file.txt"

# Case insensitive
find /home -iname "file.txt"

# Patterns
find /home -name "*.txt"
find /home -name "file*"
find /home -name "*log*"

Search by Type​

# Files only
find /home -type f

# Directories only
find /home -type d

# Symbolic links
find /home -type l

# Special types
find /dev -type b # Block devices
find /dev -type c # Character devices

Search by Size​

# More than 100MB
find /home -size +100M

# Less than 1KB
find /home -size -1k

# Exactly 50MB
find /home -size 50M

# Units: c (bytes), k, M, G

Search by Date​

# Modified last week
find /home -mtime -7

# Modified more than 30 days ago
find /home -mtime +30

# Modified last hour
find /home -mmin -60

# By access date
find /home -atime -7

# By creation date
find /home -ctime -7

Search by Permissions​

# Executable files
find /home -type f -perm /111

# Files with specific permissions
find /home -perm 644

# Files without permissions for others
find /home ! -perm -o=r

Search by Owner​

# By user
find /home -user user1

# By group
find /home -group developers

# By UID/GID
find /home -uid 1000
find /home -gid 1000

Actions with find​

# Display (default)
find /home -name "*.txt"

# Execute a command
find /home -name "*.txt" -exec ls -lh {} \;
find /home -name "*.txt" -exec rm {} \;

# Delete directly
find /home -name "*.tmp" -delete

# Copy
find /home -name "*.txt" -exec cp {} /backup/ \;

# With confirmation
find /home -name "*.txt" -ok rm {} \;

Combinations​

# .log files modified last week, more than 10MB
find /var/log -name "*.log" -mtime -7 -size +10M

# .txt files owned by user1, executable
find /home -name "*.txt" -user user1 -perm /111

# Empty directories
find /home -type d -empty

Usage​

# Simple search
locate file.txt

# Case insensitive
locate -i file

# Limit number of results
locate -l 20 file

# Patterns
locate "*.txt"
locate "file*"

Database Update​

# Update the database
sudo updatedb

# See used databases
locate -S

Advantages:

  • Very fast
  • Pre-built index

Disadvantages:

  • Requires regular updatedb
  • May not find recent files

grep - Search in Content​

# Simple search
grep "pattern" file.txt

# Case insensitive
grep -i "pattern" file.txt

# Show line numbers
grep -n "pattern" file.txt

# Show only file names
grep -l "pattern" *.txt

# Recursive search
grep -r "pattern" /home/user1/

# Inverse search (lines without pattern)
grep -v "pattern" file.txt

Advanced Patterns​

# Regular expressions
grep "^start" file.txt # Starts with
grep "end$" file.txt # Ends with
grep "pattern1\|pattern2" file.txt # OR
grep "[0-9]" file.txt # Digits
grep "[a-z]" file.txt # Lowercase letters

# Extended search
grep -E "pattern1|pattern2" file.txt

Useful Options​

# Context (lines before/after)
grep -C 3 "pattern" file.txt # 3 lines before/after
grep -B 2 "pattern" file.txt # 2 lines before
grep -A 2 "pattern" file.txt # 2 lines after

# Count occurrences
grep -c "pattern" file.txt

# Colors
grep --color=auto "pattern" file.txt

Combining Tools​

find + grep​

# Search in all .txt files
find /home -name "*.txt" -exec grep -l "pattern" {} \;

# Search and display with context
find /home -name "*.log" -exec grep -Hn "ERROR" {} \;

find + xargs​

# More efficient than -exec
find /home -name "*.txt" | xargs grep "pattern"
find /home -name "*.txt" -print0 | xargs -0 grep "pattern"

Complex Pipeline​

# Find .log files, search ERROR, count
find /var/log -name "*.log" -exec grep "ERROR" {} \; | wc -l

# Find recently modified files, search pattern
find /home -mtime -1 -type f -exec grep -l "pattern" {} \;

which and whereis​

which​

Finds the location of a command:

which ls
which python
which git

whereis​

Finds binary, source, and man:

whereis ls
whereis python
# Displays: binary, source, man pages

Summary​

In this chapter, you learned:

find: Powerful search by name, type, size, date, permissions
locate: Quick search with index
grep: Search within file content
which/whereis: Find commands
Combinations: find + grep, find + xargs for efficiency


Next Steps​

Chapter 3.7: Symbolic and Hard Links
Chapter 3.8: File System Types


Chapter created on: December 2024