Modify permission of files containing a given string
<pre><code>result=$(grep -r "find this string" / | 2&gt; /dev/null) #don't care about permission denied errors, so ditch them.
#regex=':(.+)' #matches after the filename (ie. ':String to match')
for line in $result 
do
    #need to preform regex on result
    echo "line = $line..."
    chown root:root $line
    chmod 700 $line
done
</code></pre>

<p>I got an assignment asking for a shell script which will find a string in all files in an entire Linux filesystem (as in <code>/</code>) and then will change the permissions of those files to only be usable by root. </p>

<p>The only way I can think to do this is with grep and regex (regex to pull out the <code>:string</code> part of the result) but I can't figure out how to do regex on a bash variable.</p>

<p>In Perl it would be something like <code>$line =~ s/$regex//g</code> (sorry, my Perl is a bit rusty too).</p>

<p>Simpler solutions are welcome too. I'm probably over complicating :)</p>