To append content to a file instead of overwriting it in Linux, you can use the >> operator with the cat command. Here’s how you can do it:
Command:
cat >> file
Steps:
-
Append Mode: When you run
cat >> file, it opens the specified file (file) in append mode. If the file does not exist, it will be created. -
Input Content: You can start typing the content you want to append to the file.
-
End Input: To finish entering content and save it to the file, press
Ctrl + D(EOF - End Of File) on a new line.
Example:
cat >> myfile.txt
This is an additional line.
# Press Ctrl + D to save and exit
After executing the above commands, if myfile.txt previously contained:
This is the first line.
This is the second line.
It will now contain:
This is the first line.
This is the second line.
This is an additional line.
Using >> ensures that the existing content of the file remains intact while adding new content at the end.
