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

Find 5 biggest files in Linux

I have used this command many times, but the interval between each usage is so big that I almost always forget the syntax.

So here it is:

# du -a / | sort -n -r | head -n 5

51190272	/
37705424	/root
33040524	/root/apache-tomcat-7.0.53
32802516	/root/apache-tomcat-7.0.53/logs
32802440	/root/apache-tomcat-7.0.53/logs/catalina.out