I found (on Google) this perfectly working line to replace every occurences in all files in my directory and subdirectories:
grep -lr previoustext | xargs sed -i 's/previoustext/newtext/g'
It works great.
But now I'm trying to use it in a function in my bash_aliases
file as following:
freplace() {
grep -lr previoustext | xargs sed -i 's/previoustext/newtext/g';
}
However, when I call
freplace previoustext newtext
in my terminal, nothing happens ... . The text is not replaced.
Any idea why it doesn't work ?
-Z
option ofgrep
together with the-0
option ofxargs
to prevent this kind of problems. – AdminBee Aug 12 '20 at 10:16~/bash_alaises
is not a standard file. Are you perhaps using Ubuntu? That's the only system I know of that includes that file by default. What is the output ofgrep bash_aliases ~/.bashrc
? More importantly, is that the exact function? Do you already have the stringsprevioustext
andnewtext
hardcoded in the function? If so, why are you also passing them to your function as arguments? – terdon Aug 12 '20 at 10:18