find and grep are two of the most frequently used command-line tools for locating files and searching their content — essential skills for navigating and troubleshooting a VPS efficiently.
find: Locating Files by Name, Type, or Attributes
# Find by name
find /var/www -name "*.log"
# Find by type (f=file, d=directory)
find /home -type d -name "backups"
# Find files modified in the last 7 days
find /var/log -mtime -7
# Find files larger than 100MB
find / -size +100M
Combining find with Actions
# Delete files matching a pattern
find /tmp -name "*.tmp" -delete
# Execute a command on each match
find /var/log -name "*.log" -exec gzip {} \;
{} is replaced by each matched filename; \; terminates the command for each individual file.
Finding by Permissions or Ownership
# World-writable files (security audit use case)
find / -perm -0002 -type f
# Files owned by a specific user
find /home -user john
grep: Searching File Content
# Basic search
grep "error" /var/log/app.log
# Case-insensitive search
grep -i "error" /var/log/app.log
# Recursive search across a directory
grep -r "TODO" /var/www/myapp/
# Show line numbers
grep -n "error" /var/log/app.log
Combining grep with Context
grep -B 3 -A 3 "Exception" /var/log/app.log
-B and -A show lines before/after each match — useful for seeing the surrounding context of an error, not just the single matching line.
Using grep with Regular Expressions
grep -E "error|warning|critical" /var/log/app.log
Counting Matches
grep -c "error" /var/log/app.log
Combining find and grep
find /var/www -name "*.php" -exec grep -l "deprecated_function" {} \;
Finds all PHP files, then searches each for a specific string, printing only filenames that contain a match — a common pattern for locating specific code across a large codebase.
Using grep on Compressed Log Files
zgrep "error" /var/log/app.log.gz
Searches gzip-compressed files directly without needing to manually decompress first.
A More Modern Alternative: ripgrep
sudo apt install ripgrep -y
rg "error" /var/www/myapp/
Significantly faster than traditional grep for large codebases, with sensible defaults (automatically respects .gitignore, for example) — worth installing if you frequently search large directories.
Common Errors
"Permission denied" errors cluttering find output — redirect stderr to suppress these: find / -name "*.conf" 2>/dev/null.
Continue Reading
- How to Check and Manage Disk Usage on a Linux VPS
- How to Read and Analyze Linux Logs with journalctl
- How to Monitor Real-Time System Resources with htop and top
Browse more articles in Linux Server Administration.