How to Use the find and grep Commands Like a Pro
Searching for files or text in Linux? Learn how to use find and grep — two of the most powerful commands in the terminal — to locate files, scan directories, and search inside text with precision.
Master the find and grep commands in Linux. Learn how to search files, filter results, and combine them like a pro to find exactly what you need in seconds.
🧭 Introduction: The Power of Search in Linux
When you’re managing a Linux system, you often need to find files quickly or search inside large text files for specific words or patterns.
That’s where two legendary commands come in: find and grep.
Together, they give you superpowers — you can:
- Locate files anywhere on the system
- Filter results by name, size, or modification date
- Search inside those files for specific text or code
By the end of this guide, you’ll know how to use both commands individually and in combination, just like an experienced system administrator.
🔍 1. The find Command: Locating Files Efficiently
The find command lets you search for files and directories anywhere in your system — recursively.
📘 Basic Syntax
find [path] [options] [expression]
Example:
find /home -name "notes.txt"
This searches the /home directory (and all subdirectories) for a file named notes.txt.
🔹 Common find Options
| Option | Description | Example |
|---|---|---|
-name | Search by name (case-sensitive) | find /etc -name "hosts" |
-iname | Search by name (case-insensitive) | find /etc -iname "HOSTS" |
-type | Filter by file type (f=file, d=directory) | find /var -type d |
-size | Search by size | find / -size +100M |
-mtime | Filter by modification time (in days) | find /var/log -mtime -3 |
-user | Files owned by a specific user | find /home -user wang |
-perm | Files with specific permissions | find / -perm 644 |
-exec | Execute a command on found files | find . -name "*.log" -exec rm {} \; |
🧠 Examples in Practice
Find All .log Files in /var/log
find /var/log -type f -name "*.log"
Find All Directories Named “backup”
find / -type d -name "backup"
Find Files Larger Than 500 MB
find /home -type f -size +500M
Find Recently Modified Files (within 24 hours)
find /etc -type f -mtime -1
Delete Temporary .tmp Files Older Than 7 Days
find /tmp -type f -name "*.tmp" -mtime +7 -exec rm {} \;
⚠️ Always test your command without -exec first, to confirm the result list.🔠 2. The grep Command: Searching Inside Files
If find locates files, grep digs inside them.
It searches for text patterns and prints the matching lines.
📘 Basic Syntax
grep [options] pattern [file...]
Example:
grep "error" /var/log/syslog
This displays every line containing “error” in the syslog file.
🔹 Useful grep Options
| Option | Description | Example |
|---|---|---|
-i | Ignore case | grep -i "error" logfile |
-n | Show line numbers | grep -n "failed" auth.log |
-r | Recursive search in directories | grep -r "TODO" /home/projects |
-v | Invert match (show lines not matching) | grep -v "success" report.txt |
--color | Highlight matches | grep --color "root" /etc/passwd |
-E | Enable extended regex | `grep -E "error |
🧩 Practical Examples
Find All Lines Containing “root”
grep "root" /etc/passwd
Case-Insensitive Search
grep -i "server" /etc/nginx/nginx.conf
Search Recursively in a Folder
grep -r "password" /etc/
Show Line Numbers
grep -n "listen" /etc/nginx/sites-available/default
Invert Match (Exclude a Word)
grep -v "INFO" /var/log/syslog
Highlight Results
grep --color "ERROR" /var/log/syslog
⚙️ 3. Combining find and grep (The Power Combo)
Here’s where the magic happens:
Combine both commands to search inside multiple files automatically.
🔹 Example 1: Search for Text Inside All .conf Files
find /etc -type f -name "*.conf" -exec grep -H "port" {} \;
This will:
- Find all
.conffiles under/etc - Run
grepon each file - Show filenames and matching lines
🔹 Example 2: Search for “error” in Recently Modified Log Files
find /var/log -type f -mtime -1 -exec grep -H "error" {} \;
Perfect for daily log analysis or troubleshooting.
🔹 Example 3: Search All .py Files for a Function Name
find ~/projects -type f -name "*.py" -exec grep -H "def main" {} \;
🔹 Example 4: Combine with xargs for Speed
Instead of -exec, use xargs (faster for large directories):
find /var/log -type f -name "*.log" | xargs grep "failed"
Or to handle filenames with spaces safely:
find /var/log -type f -name "*.log" -print0 | xargs -0 grep "failed"
🧩 4. Using Regular Expressions with grep
grep supports powerful regular expressions (regex) to match patterns.
Examples:
| Pattern | Description | Example |
|---|---|---|
^word | Lines starting with “word” | grep "^root" /etc/passwd |
word$ | Lines ending with “word” | grep "bash$" /etc/passwd |
^[0-9] | Lines starting with a digit | grep "^[0-9]" file.txt |
[A-Z] | Match any uppercase letter | grep "[A-Z]" names.txt |
| `error | warning` | Match “error” OR “warning” |
Example in practice:
grep -E "error|fail|critical" /var/log/syslog
This searches for any line containing error, fail, or critical — a lifesaver when debugging production servers.
🧰 5. Real-World Use Cases
Here are common admin tasks solved with find and grep:
| Task | Command |
|---|---|
| Find and delete empty files | find /home -type f -empty -delete |
| Find files modified in the last 2 hours | find /etc -mmin -120 |
| Find configuration files containing “proxy” | grep -r "proxy" /etc/ |
| Search for “root” user entries across the system | grep -r "root" / |
List all .sh files containing “chmod” command | find / -name "*.sh" -exec grep -H "chmod" {} \; |
| Audit logs for failed logins | grep "Failed password" /var/log/auth.log |
💡 6. Tips for Power Users
- Combine with
lessfor paging:grep "error"/var/log/syslog | less - Redirect output to a file:
grep "timeout"/var/log/syslog > timeouts.txt - Count matches:
grep -c "error"/var/log/syslog - Highlight multiple patterns:
grep -E --color "error|fail|critical"/var/log/syslog - Debug large searches with
time:time find / -type f -name "*.conf" -exec grep -H "server"{} \;
🔄 7. Troubleshooting Common Mistakes
| Issue | Cause | Fix |
|---|---|---|
| “Permission denied” | Searching system dirs without sudo | Add sudo to your command |
| “Argument list too long” | Too many results for xargs | Use -print0 and xargs -0 |
| “No such file or directory” | Typo in path | Double-check your target path |
| “Binary file matches” | grep found non-text files | Add --text to force text search |
🧭 Summary
The combination of find + grep is one of the most powerful tools in the Linux toolbox.
It allows you to:
- Find files based on names, dates, and permissions
- Search inside files for specific patterns or keywords
- Automate cleanups, audits, and reports
Mastering these two commands will make you dramatically faster, more precise, and more confident on the command line.
💡 Pro tip: Add these to your daily workflow — you’ll soon wonder how you ever managed without them.
🧭 Next Steps
Keep building your Linux command-line mastery:
- Top 20 Essential Linux Commands Every User Should Know
- Monitoring System Performance with top, htop, and iostat
- How to Secure SSH Access on Your Linux Server
Or dive deeper with our full “Linux Command-Line Mastery” eBook on dargslan.com.