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.

How to Use the find and grep Commands Like a Pro
How to Use the find and grep Commands Like a Pro

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

OptionDescriptionExample
-nameSearch by name (case-sensitive)find /etc -name "hosts"
-inameSearch by name (case-insensitive)find /etc -iname "HOSTS"
-typeFilter by file type (f=file, d=directory)find /var -type d
-sizeSearch by sizefind / -size +100M
-mtimeFilter by modification time (in days)find /var/log -mtime -3
-userFiles owned by a specific userfind /home -user wang
-permFiles with specific permissionsfind / -perm 644
-execExecute a command on found filesfind . -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

OptionDescriptionExample
-iIgnore casegrep -i "error" logfile
-nShow line numbersgrep -n "failed" auth.log
-rRecursive search in directoriesgrep -r "TODO" /home/projects
-vInvert match (show lines not matching)grep -v "success" report.txt
--colorHighlight matchesgrep --color "root" /etc/passwd
-EEnable extended regex`grep -E "error

🧩 Practical Examples

Find All Lines Containing “root”

grep "root" /etc/passwd

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:

  1. Find all .conf files under /etc
  2. Run grep on each file
  3. 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:

PatternDescriptionExample
^wordLines starting with “word”grep "^root" /etc/passwd
word$Lines ending with “word”grep "bash$" /etc/passwd
^[0-9]Lines starting with a digitgrep "^[0-9]" file.txt
[A-Z]Match any uppercase lettergrep "[A-Z]" names.txt
`errorwarning`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:

TaskCommand
Find and delete empty filesfind /home -type f -empty -delete
Find files modified in the last 2 hoursfind /etc -mmin -120
Find configuration files containing “proxy”grep -r "proxy" /etc/
Search for “root” user entries across the systemgrep -r "root" /
List all .sh files containing “chmod” commandfind / -name "*.sh" -exec grep -H "chmod" {} \;
Audit logs for failed loginsgrep "Failed password" /var/log/auth.log

💡 6. Tips for Power Users

  1. Combine with less for paging:grep "error" /var/log/syslog | less
  2. Redirect output to a file:grep "timeout" /var/log/syslog > timeouts.txt
  3. Count matches:grep -c "error" /var/log/syslog
  4. Highlight multiple patterns:grep -E --color "error|fail|critical" /var/log/syslog
  5. Debug large searches with time:time find / -type f -name "*.conf" -exec grep -H "server" {} \;

🔄 7. Troubleshooting Common Mistakes

IssueCauseFix
“Permission denied”Searching system dirs without sudoAdd sudo to your command
“Argument list too long”Too many results for xargsUse -print0 and xargs -0
“No such file or directory”Typo in pathDouble-check your target path
“Binary file matches”grep found non-text filesAdd --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:

Or dive deeper with our full “Linux Command-Line Mastery” eBook on dargslan.com.