Shell script to find and replace contents of files in directory

Hi all

This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky for this simple task and i started to shrink it. I was struck with this line here.

sed '/s/abc/def/g' > tmp/copy.tmp
mv tmp/copy.tmp $file

This piece of code is working absolutely fine but when i tried to redirect output directly to the $file then $file is nullified. can someone please explain me why this is happening and is there a way to replace string in just one line of code?

sed '/s/abc/def/g' > $file

Many Thanks & Regards,
Raj

If you open the file for output, that overwrites it before reading the content, so although no input is then found so there is no processing, the damage has been done.

We've all wondered about this at some point, so don't apologise.

I hope that this helps,
Robin
Liverpool/Blackburn
UK

1 Like

Thanks a lot rbattle1 for your quick reply. Your answer solved my confusion....