• Arun Kumar

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/

103 views

Recent Posts

See All