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 ?
-Zoption ofgreptogether with the-0option ofxargsto prevent this kind of problems. – AdminBee Aug 12 '20 at 10:16~/bash_alaisesis 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 stringsprevioustextandnewtexthardcoded in the function? If so, why are you also passing them to your function as arguments? – terdon Aug 12 '20 at 10:18