• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

AI, Linux, Cloud and DevOps

  • Home
  • Resources
    • Learn Linux
    • Articles
  • My Projects & Work
    • My Nagios Plugin
    • My Dockerhub Repo
    • My WordPress plugins
    • My Github Repos
  • Facebook Page
  • Contact
  • Privacy Policy
  • About Me
You are here: Home / Articles / Taking file backup while using sed command with -i flag

Taking file backup while using sed command with -i flag

July 12, 2014 by Sharad Chhetri 2 Comments

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 -i .

As per the manual page of sed command (Run `man sed` command in terminal) , the -i option is used for ‘edit files in place’ .

-i[SUFFIX], –in-place[=SUFFIX]
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.

Syntax

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.

EXAMPLE

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.

sed

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook
  • More
  • Print (Opens in new window) Print
  • Email a link to a friend (Opens in new window) Email
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Mastodon (Opens in new window) Mastodon

Articles

remove html tags by using sed command
Use Linux Date Command As A Timezone Converter
sed command to display text between two strings or keywords
How to print particular line number by using sed command
Change path of tmpdir in MySQL Server On Ubuntu
cat,sed and awk commands to display file contents
create and delete user in Red Hat and CentOS
type Command : Display Information About Command Type On Linux
How to backup and restore iptables on CentOS 7 / RHEL 7
How to create OpenSSH rpm package and its upgrade
Essential Linux Commands Every Beginner Should Know | With Examples
Learn Linux Date Command With Examples
4 Linux commands to get number of lines in file
Cassandra backup script on linux system
sed : find the pattern (keyword) and delete the line from file

Filed Under: Articles Tagged With: sed

Reader Interactions

Comments

  1. Tatsh says

    July 17, 2014 at 9:47 am

    Don’t use “. That is deprecated as of a while ago, at least in Bash. POSIX standards say both can be used (which means if you have a machine that is not Bash but has a POSIX-standard shell, both should work). If you see “, please change it ๐Ÿ˜‰ It gets to be very difficult to escape ` within “, unlike $() where you can keep on double quoting in the shell, and you do not need any quotes for assignment to a variable:

    local_var=$(my command “some string” $(another command))
    equivalent “: local_var=`my command “some string” `another command“ (hard to read and hard to remember such rules, much like trying to escape ‘ in ” (single quotes))

    http://wiki.bash-hackers.org/syntax/expansion/cmdsubst
    https://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution
    http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_03

    Reply
    • sharadchhetri says

      July 17, 2014 at 12:06 pm

      Thankyou Tatsh,

      Very well said. You really made a good point.
      Thanks for highlighting

      Regards
      Sharad

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Articles

  • Install n8n docker in Ubuntu for practicing
  • Bash Script: MySQL Database backup to AWS S3
  • Automate no touch Jenkins setup wizard on docker (jcac)
  • How to run and configure Jenkins in Docker container
  • Complete Guide: How to install Nagios Monitoring on AlmaLinux / Rocky Linux
  • Install KVM on Ubuntu 24.04 LTS ( Bash Script )
  • How to install GIMP 2.10 on Ubuntu / Debian
  • 5 ways to check Ubuntu version of system (commands and GUI)
  • How To Solve Forgot MySQL / MariaDB Root Password
  • Create Self Hosted Build Agent Docker Image for Azure DevOps

Copyright © 2026 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From India.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy