top of page

Shell Script to Find Text Files Containing Word 'user_defind'

Search for files containing specific text using a shell script.

Linux shell script allows user to find the word containing in files in a directory. In order to achieve this, we are using the find and grep command. Here the user will be asked to enter the text to search.


vi search_in_files.sh
# !/bin/bash
echo "Type the word to find text files"
read fn
find . -exec grep -l "$fn" {} \;

Here is the sample output of the above script

Shell Script to Find Text Files - search in files.sh


More ways to use find and grep

  • You can select only those lines containing matches that form whole words using the -w option.

  • You can also use this to search for two or more words - egrep -w -R 'word1|word2' ~/projects/

Become a top notch dba with DBA Genesis

Become a top notch DBA.

Unlimited Courses & Xperiments!

bottom of page