Linux: using grep + find to locate stuff fast

Configurare noua (How To)

Situatie

In Linux, grep and find are two powerful command-line tools that, when combined, allow you to quickly search and locate files and content on your system.

  • find helps you search for files and directories based on various criteria, such as name, size, modification date, and more.

  • grep is used to search for specific content within files, making it perfect for finding text patterns or keywords inside files.

it’s a must-know skill for navigating and searching in Linux
Goal:
Find specific files or content inside files — fast.

Solutie

A. Finding Files by Name with find

# Find all files named “config.php” starting from root
find / -name “config.php” 2>/dev/null

#(info:2>/dev/null hides permission errors.)

# Find all .txt files in your home directory
find ~/ -type f -name “*.txt”

# Find files modified in the last 1 day
find /var/log -type f -mtime -1

B. Finding Text Inside Files with grep

# Recursively search for “password” in files under /etc
grep -rnw ‘/etc’ -e ‘password’

# r = recursive, n = show line number, w = match whole word

# Search for “admin” in all .log files
grep “admin” *.log

# Case-insensitive search
grep -i “username” /etc/passwd

Combo Time: find + grep

# Find all .php files and search for “mysql” inside them
find . -name “*.php” -exec grep -inH “mysql” {} \;
-exec runs a command on each file found.

{} is a placeholder for the filename.

\; ends the command.

Example Use Case
Let’s say you’re on a compromised machine and want to look for credentials:

# Look for “password” in all readable .env files
find / -name “.env” 2>/dev/null -exec grep -i “password” {} \;

Tip solutie

Permanent

Voteaza

(10 din 18 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?