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](https://static.wixstatic.com/media/15aea5_a28156555e624fe2a9263759b7c8363a~mv2.png/v1/fill/w_66,h_50,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/15aea5_a28156555e624fe2a9263759b7c8363a~mv2.png)
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/
bottom of page