by JoeAcchino · 13y ago · 0 comments
find ~/my/docs/ -type f -name '*.txt' -exec sed -i.bak 's/inheritance/composition/g' {} +
This will search all files ending in `.txt` and will exec `sed 's/inheritance/composition/g'` on it. Sed modifies files in-place and saves a backup file with .bak extension (-i.bak) and is called the minimum necessary times (-exec +).My best advice to someone using the command line is to learn `find` + `xargs` or, even better, `find` + `parallel`.
# bash
# find/replace
function fr {
local pattern=$1
local replacement=$2
local program
if [[ -z "$replacement" ]]
then
program="grep $pattern"
else
program="sed -i s/$pattern/$replacement/g"
fi
while read line
do
$program "$line"
done
}
# usage
find . | fr find-this
find . | fr replace-this with-that