Today this is another post on sed command in our blog. sed which is one of the famous and dynamic stream line editor use in Unix/Linux Operating System .
Along with sed command we can use many available option. One of the option is
As per the manual page of sed command (Run `man sed` command in terminal) , the -i option is used for ‘edit files in place’ .
edit files in place (makes backup if SUFFIX supplied)
In common statement , insert the pattern/keyword Or edit into the file.s Without -i option the sed command shows the result output without editing or changing the original file content.
As a System Admin, it is common practice to take backup of file on which you are doing changes.
On fly we take backup commonly by using copy command (
cp -p filename filename.date.bak)
We can do first backup of file and then use -i option with sed command to do changes in file.
Here is a time saving sed command for you. It will take backup automatically alongside you can do changes in file.
sed "-i.bak" 's/Pattern-1/Pattern-2/g' /path/of/filename
NOTE: We are not limited with .bak suffix . We can use any suffix which is relevant to recognise file backup.
I mostly use date command.Which helps me to recognise on which date the file was taken backup.
Here I will use date command in suffix to print today’s date
sed "-i.`date +%F`" 's/Pattern-1/Pattern-2/g' /path/of/filename
Screenshot taken from my system.