Here is simple solution using a temporary file to prepend text: In the following script, an existing file, books.txt is assigned to the variable, filename, and a string value will be taken as input from the user to add at the end of the file. If the input value is not empty, then the ‘echo’ command will append the value into the books.txt file by using ‘>>’ symbol. sed "i" command lets us insert lines in a file, based on the line number or regex provided. To add two lines of text, separate each line with the \n option: printf ‘First line of text\n Second line of text’ test6.txt. Here is my sample file # cat /tmp/file Line One Line Two Line Three Line Four Line Five . Add character at the beginning of each line using sed command. From time to time it is required to modify some file very fast. But the issue with this command is that it is appending the first line of the file and traversing entire file. A single number selects that one line. To add a single line of text, enter: printf ‘First line of text\n’ test5.txt. 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. To start the editor in a Linux system, type vi followed by the name of the file you want to edit, like this: vi /etc/fstab The vi editor loads the file into memory, displays the first few lines in a text screen, and positions the cursor on the first line. Bash prepend a text using a temporary file. Sometimes you may be required to write or append multiple lines to a file. Note the comma between 1 and 4. */PREFIX: &/p' /tmp/file PREFIX: Line One PREFIX: Line Two PREFIX: Line Three PREFIX: Line Four PREFIX: Line Five For the last line it's again traversing the entire file and appending a last line. To extract lines one to four, we type this command: sed -n '1,4p' coleridge.txt. sed "a" command lets us append lines to a file, based on the line number or regex provided. In the following article, you’ll find an information about how to add some text, character or comma to the beginning or to the end of every line in a file using sed and awk. You can use the cat command on either of these files to display their contents. echo "$(echo 'task goes here' | cat - todo.txt)" > todo.txt It's impossible to add lines to the beginning of the file without over writing the whole file. If you need to add a line to a file in Linux, and you need to add that line in a specific position of the file there is an easy solution, even if you need to do it to hundreds of thousands of files. Add a line in a specific position with Linux. Example-1: Append line to the file using ‘echo’ command and ‘>>’ symbol. The p means “print matched lines.” By default, sed prints all lines. If the text file is small enough to fit in memory, you don't have to create a temporary file to replace it with. Consider this file: line 1 line 2 line 4 Method 1:-You can write/append content line by line using the multiple echo commands. Append a prefix in front of every line of a file. 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. Here we will add a text “PREFIX:” in front of every line of my file # sed -ne 's/. Linux: Using sed to insert lines before or after a match The sed utility is a powerful utility for doing text transformations. And it goes without saying that the most popular command line tools for this in Linux are sed and awk – the two best text processing programs.. So, the lines will be added to the file AFTER the line where condition matches. Here are the three methods described below. You can load it all into memory and write it back out to the file. To select some lines from the file, we provide the start and end lines of the range we want to select. Since its very huge file (14GB) this is taking very long time. You can use multiple methods to write multiple lines to a file through the command line in the Linux system. In this article, I will provide an example of how to insert a line before and after a match using sed, which is a common task for customizing configuration files.