Linux Tips and Tricks

Linux Tips and Tricks

Find and Replace with SED


Normally we use SED command to replace one word to another.

The following example will replace the word old with the word new in the test.txt file

$ echo “old is old” > test.txt

$ cat test.txt
old is old

$ sed -i ‘s/old/new/’ test.txt

$ cat test.txt
new is old

Note : The above sed command only replaced the first occurance. If you want to replace all the occurance in the line. you have use ‘g’

$ sed -i ‘s/old/new/g’ test.txt

Now, we see how to replace in all the files from the current directory.

$ find . -type f -exec sed -i ‘s/old/new/g’ {} \;

Here is the sample script to take backup of the original file before you do the changes with SED command.

for i in *; do
cp $i $i.bak
sed -i ‘s/old/new/g’ $i

SED tutorial:

posted under Uncategorized

Email will not be published

Website example

Your Comment:

Recent Comments