Linux Shell Script to check for string inside a file?

This is what I have so far

grep -lir "some text" *

I need to check all files on the system for this text, if the text is found I need to change the file permissions so only ROOT has read and write access.

How can this be done?

grep -lir "some text" * |
 while IFS= read -r file
 do
    chown root "$file"
    chmod go-rw "$file"
 done
1 Like

Thanks very much! Exactly what I was looking for :slight_smile:

How could I modify so instead of being statically set to "some text" it accepted some input text into the prompt for the search of this text.

Thanks!

Either accept the text as a parameter on the command line and use the positional parameters:

grep -lir "$1" * |
 ...

or prompt for the text:

printf "Enter string to search for: "
IFS= read -r text
grep -lir "$text" * |
 ...
1 Like