Find and replace text within a file using commands on Linux

How can I find and replace specific words in a text file using command line?

cd /path/to/your/folder/nikeshshakya
sed -i ‘s/original/new/g’ file.txt
Explanation:

sed = Stream EDitor
-i = in-place (i.e. save back to the original file)
The command string:

s = the substitute command
original = a regular expression describing the word to replace (or just the word itself)
new = the text to replace it with
g = global (i.e. replace all and not just the first occurrence)
file.txt = the file name

Or to make replace on all files on folder
cd /path/to/your/folder/nikeshshakya
sed -i ‘s/foo/bar/g’ *

Comments are closed.