Linux: Locate a file by name and then search for a specific word inside

If you’ve ever needed to locate a file by name and then search for a specific word inside it, then this blog is for you.
Linux makes it simple by combining two powerful tools: find and grep:

# find /your/path -type f -name "*.log" -exec grep -i "error" {} +

Explanation:

  • -type f: Filters for files only.
  • -name "*.log": Limits the search to .log files.
  • -exec grep -i "error" {} +: Searches for the word "error" inside each found file, ignoring case sensitivity.

In my case, I was searching for files named flashgrid_node and then wanted to find content containing the keyword “SYNCING“. Here is my command version:

# find ./ -type f -name "flashgrid_node" -exec grep -i "SYNCING" {} +

It searches in the current directory (‘./’).

Useful tip, If you want to show only the file names that contain the word, you can add the -l flag to grep:

# find /your/path -type f -name "*.log" -exec grep -il "error" {} +

This was my output:

$ find ./ -type f -name "flashgrid_node" -exec grep -il "SYNCING" {} +

./rac1/rac1.example.com/flashgrid_node
./rac2/rac2.example.com/flashgrid_node
./racq/racq.example.com/flashgrid_node

Unknown's avatarAbout Mariami Kupatadze
Oracle Certified Master Linkedin: https://www.linkedin.com/in/mariami-kupatadze-01074722/

Leave a Reply