Ubuntu – Finding file and replacing word with some specific word

command linetext processing

Find some specific file and then once a file is found, I want some specific word in it and then replace it with finding word with a new one. Can I do it with a single line?

For example, I want to find a file 'file.txt' and in that file want to search read and replace it with write using a single-line command.

Best Answer

Execute:

find -type f -name "specificFileName" -exec sed -i.bak 's/foundWord/replaceWithWord/' '{}' \;

This will only replace first seen 'foundWord' with 'replaceWithWord', in case you wanted to replace all these 'foundWord's add sed's "g" enabler to having replace all seen

find -type f -name "specificFileName" -exec sed -i.bak 's/foundWord/replaceWithWord/g' '{}' \;

note: This will copies the 'specificFileName's as their backup files and name them with "specificFileName.bak", by dropping the "-i.bak" you will tell 'sed' to inplace replace,