Luckily, there are several Linux commands you might use to do this.
Some of these commands are even powerful enough to insert text anywhere in your file.
Create a Text File First!
Hannah Stryker / How-To Geek
For example, we’re going to name it append.txt.
Now that we have an empty file, we can add some lines to it.
Do the same for “filename.”
Let’s see that in action.
launch the below command:
So how to overcome this?
Instead, it’s possible for you to append the contents of one file to another.
you gotta combine them into a single file.
To do that, run:
3.
But did you know thatprintfis a Linux tool as well?
As the name suggests, it lets you print text or data to the standard output.
This means you should probably do it manually.
it’s possible for you to use the “\n” escape character at the end of your text.
So let’s use that now.
This time, the terminal prompt is at a new line, unlike in the previous case.
Using sed
Thesed command, short for stream editor, performs basic text transformation on inputs.
There are multiple ways to use this command.
The “$” sign means the line will be added at the end of the file.
Either use the “-a” option or the double redirection operator.
Let’s take a look at the first method.
After executing the command, you will enter interactive mode.
you’re able to write any text and press Enter to append it to the file.
Upon doing so, you will see the same text as output on the terminal.
To exit the prompt, press Ctrl+D.
Other than that, it works in the same way.
Using awk
Theawk commandis extremely powerful when it comes to text manipulation.
With some tweaking, you could add text to files using this command.
This one is rather straightforward.
For this tutorial, we’ll use thenano text editor.
If you better create a new line, do that by pressing Enter.
Then enter any text you want.
Finally, save the file using Ctrl+O and exit the editor using Ctrl+X.
Suppose you run thels commandto get the contents of the present directory.
You want to save the list to a file called command.txt.
For that, launch the below command:
The same goes for any otherLinux command.
1 and 2 are file descriptors for standard output and standard error, respectively.