copied to clipboard!
string sed

[sed] Run and understand Create backup and add to file

created-2026/03/25 updated-2026/03/25

Introduction

This repository runs the sed command to create a backup and then appends a string to a file.

Commands that need to be installed

  1. git

Quick Start

If you have already installed the above command, run the following command to download from the repository, then navigate to the directory and run the sed command to confirm that the string has been added to the file and that a backup has been created.

Ubuntu

git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i'.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Output results
aaa
bbb
ccc
ddd
eee

aaa
fff
bbb
ccc
ddd
eee

test1.txt test1.txt.bak

macOS

MacPorts

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i '.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Output results
aaa
bbb
ccc
ddd
eee

aaa
fffbbb
ccc
ddd
eee

test1.txt test1.txt.bak

Windows

WSL2

git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i'.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Output results
aaa
bbb
ccc
ddd
eee

aaa
fff
bbb
ccc
ddd
eee

test1.txt test1.txt.bak

MSYS2 MINGW64

git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i'.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Output results
aaa
bbb
ccc
ddd
eee

aaa
fff
bbb
ccc
ddd
eee

test1.txt test1.txt.bak

Execution steps

Ubuntu

Click to open details

Preparation

Enter the following command into your terminal. If you don’t get a “command not found” message, then it’s OK.

git --version

preinstall

If the command you see as “command not found” in your terminal, please install it using the following command.

sudo apt install git

command

Running the following command in your terminal should output a string similar to the details shown.

git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i'.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Detail
aaa
bbb
ccc
ddd
eee

aaa
fff
bbb
ccc
ddd
eee

test1.txt test1.txt.bak

macOS

Click to open details

Preparation

If you enter the following command into the terminal and you don’t get a “command not found” message, then it’s OK.

source ~/bashrc_folder/macports_alias
git --version

*macOS uses the MacPorts package manager to manage commands. If you haven’t installed it yet, please see the MacPorts installation instructions at the link below. Also, you can set aliases for commands so that they can be called without conflicting with your existing environment. If this is your first time using this blog, please refer to the following two articles to set up your environment.

preinstall

If you encounter a “command not found” error in the terminal, install the command using the following command and then set up an alias for it.

sudo port install git
echo 'alias git="/opt/local/bin/git"' >> ~/bashrc_folder/macports_alias

command

Running the following command in the terminal should output a string similar to the details shown.

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i '.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Detail
aaa
bbb
ccc
ddd
eee

aaa
fffbbb
ccc
ddd
eee

test1.txt test1.txt.bak

Windows

Click to open details

Preparation

If you enter the following command into the prompt and you don’t get a “command not found” message, then it’s OK.

WSL2
git --version
MSYS2 MINGW64
git --version

*This has been tested on Windows using WSL2 and MSYS2 MINGW64. Please install WSL2 if possible. If you are unable to set it up, you can install MSYS2, but some commands may only work with WSL2. Please refer to the article below for instructions on how to install WSL2 and MSYS2.

preinstall

If you see a “command not found” error in the prompt, please install the command using the following command.

WSL2
sudo apt install git
MSYS2 MINGW64
pacman --sync git

command

Executing the following command at the prompt should output a string similar to the details shown.

WSL2
git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i'.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Detail
aaa
bbb
ccc
ddd
eee

aaa
fff
bbb
ccc
ddd
eee

test1.txt test1.txt.bak
MSYS2 MINGW64
git clone https://github.com/trygfmi/sed_create-backup
cd sed_create-backup
cat test1.txt
sed -i'.bak' '2i\
fff' test1.txt
cat test1.txt
ls
Detail
aaa
bbb
ccc
ddd
eee

aaa
fff
bbb
ccc
ddd
eee

test1.txt test1.txt.bak

In conclusion

Including the backup filename in the argument, as shown below, makes the filename quite long, so just including the extension should suffice.

Ubuntu, Windows

sed -i'test1.bak' '2i\
fff' test1.txt
ls

macOS

sed -i 'test1.bak' '2i\
fff' test1.txt
ls

List of how to use the sed command

In addition to this article, there are other articles about the sed command, which are introduced at the following link, so please take a look if you’re interested.

Leave a Reply

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

©︎ 2025-2026 todo