File Editing: Appending a Non-Existent Line to a File


Appending a line or string to a file is sometimes necessary to change the output without deleting the existing data. It is a valuable way to modify a file by adding several lines between the existing ones of a file.

"append" means adding the data into a file without erasing currently available data. To append a line in the file, you can use various commands such as printf, echo, tee, cat, etc.

Although appending a line is easy, many beginners always require clarification. So this tutorial will include the simple approaches for appending a non-existent line to a file.

File Editing: Appending a Non-Existent Line to a File

The '>>' operator can redirect the file's output. If the mentioned file does not exist, it automatically creates it and then adds the line. With this operator, you can add a new line to a file using multiple commands.

You will print the lines with the help of commands, and the '>>' operator will print this line in the file. For example, we have a file, 'Linux.txt,' that contains the following information −

:~$ cat Linux.txt 
  What does append mean in Linux?

In this file, we will append a new line with the help of several different commands.

The printf Command

The 'printf' command lets you write, format, and convert argument parameters to standard output. First, we will see how to add a new line to a file by using the '>>' operator with the printf command −

:~$ printf "<Text_line> 
" >> <file_name>

Let's now add a new line in the 'Linux.txt' file by using the following command −

:~$ printf "You can append a line through various commands in the terminal. 
" >> Linux.txt :~$ cat Linux.txt What does append mean in Linux? You can append a line through various commands in the terminal.

You can see that running the above command adds a new line to the file.

Echo Command

Using the 'echo' built-in command, you can display a string/line of text in Linux by passing it as an argument.

:~$ echo "<new_text>" >> <file_name>

Let's try to add the line again in the Linux.txt −

:~$ echo "You can append a line through various commands in the terminal. 
" >> Linux.txt :~$ cat Linux.txt What does append mean in Linux? You can append a line through various commands in the terminal.

Tee Command

The 'tee' command in Linux reads the input and prints it to one or more files. Through this command, you can print the input of one file in another file. Moreover, you can also use the tee command to directly print a new line in the file.

:~$ cat <source file_name> | tee -a <target file_name>

For example, we will print the matter of the 'Info.txt' file in the 'Linux.txt' file with the help of this command. Here we first take the line present in the 'Info.txt' file as input with the cat command. Furthermore, we will pipe the tee command to print this input in the 'Linux.txt' file.

:~$ cat Info.txt | tee -a Linux.txt
You can append a line through various commands in the terminal.
:~$ cat Linux.txt 
What does append mean in Linux? 

You can append a line through various commands in the terminal.

Apart from it, you can directly append a new line to the file using the echo command. The same way is to print the line with the echo command and then pipe it with the tee command.

:~$ echo "<text_line>" | tee -a <file_name>

In the above command, the -a option appends a line of the given file. Let's implement this combination of commands to the file −

:~$ echo "You can append a line through various commands in the terminal." | tee -a Linux.txt
You can append a line through various commands in the terminal.
:~$ cat Linux.txt
What does append mean in Linux? 

You can append a line through various commands in the terminal.

Cat Command

The powerful and universal cat command is primarily used to display file contents. Along with this, you can print the content from one file to another −

:~$ cat <source file_name> >> <target file_name>

Here again, we will append the content of the 'Linux1.txt' file in the 'Linux.txt' file with the help of the cat command.

:~$ cat Info.txt >> Linux.txt
:~$ cat Linux.txt
What does append mean in Linux? 

Append means simply add the data to the file erasing existing data.

Conclusion

This was about simple ways to append a non-existent line to a file. We have explained four methods to append a line without modifying the existing output. Please ensure you use all the commands correctly because inappropriate commands can sometimes lead to errors. Additionally, you can find out more about Linux by visiting our official website.

Updated on: 18-May-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements