How to Find Files and Search File Content with find and grep

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

Browse more articles in Linux Server Administration.

  • find command linux, grep tutorial, search files linux, ripgrep
  • 0 Корисниците го најдоа ова како корисно
Дали Ви помогна овој одговор?

Понудени резултати

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...