top of page

Shell Script to Find Text Files Containing Word 'user_defind'

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/

Related Posts

Heading 2

Add paragraph text. Click “Edit Text” to customize this theme across your site. You can update and reuse text themes.

bottom of page