You can combine two -exec
operation of find
, firstly to find all files containing matching string and secondly to replace the string with replace keyword (keeping backup of original file), like
find . -type f -exec grep -il "searchstring" {} \; -exec sed -i.bak \
's/searchstring/replacestring/' {} \;
Explanation :
-exec grep -il "searchstring"
: search for "searchsring"
-exec sed -i.bak 's/searchstring/replacestring/'
: If any files found containing string then keep backup of file their and replace with "replacestring".
Note that in this case the second command will only run if the first one returns successfully.
sed -i.orig 's/abc/xyz/' <list of files>
see https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files for detailed discussion on replacing strings – Sundeep Jul 13 '16 at 13:57<list of files>
can be something like$(grep -l abc *.txt)
to get list of files ending with .txt containing the string abc – Sundeep Jul 13 '16 at 14:04