BASH Prepend A Text / Lines To a File

BASH Prepend A Text / Lines To a File

As far as I know there is no prepend operator on a bash or any other shell, however there are many ways to do the same. You can use ed, sed, perl, awk and so on to add text to the beginning of a file in Bash under Linux or Unix-like systems.

I am a new Unix system user with a bash as a default shell. I can append text to a file using >> operator but how do I prepend a text to a file? I want the opposite operation of >> . How can I prepend some data to a text file? How do we add text to the beginning of a file in Bash under Linux?

Linux and Unix command to prepend text to a file

ADVERTISEMENTS

As far as I know there is no prepend operator on a bash or any other shell, however there are many ways to do the same. You can use ed, sed, perl, awk and so on to add text to the beginning of a file in Bash under Linux or Unix-like systems.

Bash prepend a text using a temporary file

Here is simple solution using a temporary file to prepend text:

echo 'line 1' > /tmp/newfile
echo 'line 2' >> /tmp/newfile
cat yourfile >> /tmp/newfile
cp /tmp/newfile yourfile

Here is one line solution:

echo "text"|cat - yourfile > /tmp/out && mv /tmp/out yourfile
echo "nixCraft"|cat - yourfile > /tmp/out && mv /tmp/out yourfile

BASH Prepend A Text or Lines To a File Under Linux and UnixPrepending A Text or Lines To a File Under Linux and Unix

Using bash only solution to add text to the beginning of a file

No need to create a temp file. The syntax is:

echo -e "DATA-Line-1\n$(cat input)" > input
cat input

To add multiple lines:

echo -e "DATA-Line-1\nDATA-Line-2\n$(cat input)" > input
cat input

For example add text to the beginning of a text file called input using bash as follows:

cat input
echo -e "Famous Quotes\n$(cat input)" > input

Linux bash append text at file begin

Use sed command to prepend data to a text file

The syntax is follows to prepend A text or lines to a file when we use the sed command:

sed  '1s;^;DATA-Line-1\n;' input > output
### GNU/sed syntax ##
sed -i '1s;^;DATA-Line-1\n;' input
### Verify it using the cat ##
cat input

Here is a sample input file:

$ cat input.txt
This is a test file.
I love Unix.

Next prepend two lines (make sure you add \n):

$ sed -i '1s;^;Force-1\nForce-2\n;' input.txt
$ cat input.txt
Force-1
Force-2
This is a test file.
I love Unix.

How do I prepend a string to the beginning of each line in a file?

The awk command syntax is:

awk '{print "Line-1" $0}' file
### add a new line for each matched line ##
awk '{print "~~~~~~~~\n" $0}' quotes.txt > output.txt

Display result using the [nicmd name=”cat”] or grep command/egrep command:

$ cat output.txt

The sed syntax is:

sed -i -e 's/^/Line-1/' file

awk pretend text

linux

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated Linux Developer

Looking to develop real-time applications? **[Hire Dedicated Linux Developer](https://hourlydeveloper.io/hire-dedicated-linux-developer/ "Hire Dedicated Linux Developer")** from **[HourlyDeveloper.io](https://hourlydeveloper.io/...

How I Switched from Windows 10 to Linux Mint

This article is all about my journey on switching from Windows 10 to Linux Mint 20, how I got easily adapted to the Linux environment, and some resources that helped me to set up a perfect Desktop environment.

User Administration in Linux - Linux Tutorial - Linux Training

User Administration in Linux - Linux Tutorial - Linux Training

Package Management in Linux - Linux Tutorial - Linux Training

Package Management in Linux - Linux Tutorial - Linux Training

10 Best Ubuntu-based Linux Distributions

Ubuntu is arguably one of the most popular and widely-used Linux distribution owing to its classic UI, stability, user-friendliness, and a rich repository that contains over 50,000 software packages. Furthermore, it comes highly recommended for beginners who are trying to give a shot at Linux.