Append Lines to File Linux: 5 Easy Ways
In this article
- What Does "Append" Mean in Linux?
- Method 1: Using echo with Redirection
- Method 2: Using printf for Formatting
- Method 3: Using cat with a Here Document
- Method 4: Using tee -a
- Method 5: Using sed or awk (Advanced)
- How to Choose the Right Method
- Common Problems and Solutions
- Frequently Asked Questions
- Safety and Best Practices
To append lines to a file in Linux, you use the >> redirection operator with echo, printf, or cat, or you use the tee -a command. For example, echo "new line" >> file.txt adds a line to the end of file.txt without overwriting existing content. This article explains each method, when to use it, and common pitfalls.
What Does "Append" Mean in Linux?
In Linux, every file is just a stream of bytes. When you open a file for writing with >, the shell truncates (empties) the file first. Appending with >> opens the file in append mode, so the write pointer starts at the current end of the file. This preserves existing data and adds new data after it. The append operation is atomic for small writes on most filesystems, meaning concurrent processes won't interleave partial lines.
Method 1: Using echo with Redirection
The most common way to append a single line is:
echo "This is a new line" >> /path/to/file.txtIf you want to append multiple lines, use echo with -e to interpret escape sequences like \n:
echo -e "Line 1\nLine 2\nLine 3" >> file.txtNote that echo automatically adds a newline at the end, so each command appends a complete line. To append without a trailing newline, use echo -n.
Method 2: Using printf for Formatting
printf gives you more control over formatting. It doesn't add a newline unless you include \n:
printf "%s\n" "Line 1" "Line 2" >> file.txtThis appends two lines. You can also use printf to append a line with variables:
name="Alice"; printf "Hello, %s!\n" "$name" >> file.txtMethod 3: Using cat with a Here Document
For appending multiple lines or even entire blocks of text, a here document is clean:
cat >> file.txt << EOF
Line one
Line two
EOFThe >> tells cat to append, and the here document feeds the lines until the delimiter (EOF) is seen. This is ideal for configuration files or logs. You can also use cat to append the contents of another file:
cat other.txt >> file.txtMethod 4: Using tee -a
tee normally writes to both stdout and a file, overwriting it. With -a, it appends instead:
echo "new line" | tee -a file.txtThis prints the line to the terminal and appends it to the file. It's useful when you want to see what you just appended, especially in scripts.
Method 5: Using sed or awk (Advanced)
For appending lines conditionally or at a specific pattern, sed and awk can do it. For example, to append a line after every line containing "foo":
sed -i '/foo/a\new line' file.txtOr to append at the end of the file using awk:
awk '{print}' file.txt > tmp && echo "new line" >> tmp && mv tmp file.txtBut for simple appending, the earlier methods are simpler and safer.
How to Choose the Right Method
- Single line, simple text:
echo "text" >> file - Formatted output or variables:
printf - Multiple lines or block of text: here document with
cat - Need to see the output:
tee -a - Conditional appending:
sedorawk
Common Problems and Solutions
Permission Denied
If you get "Permission denied", you don't have write access to the file or directory. Use sudo only if you understand the risks, or change ownership with chown.
Accidentally Overwrote a File
Using > instead of >> is a classic mistake. Always double-check your redirection. There's no undo in the shell, but if you have a backup or use version control, you can recover.
Appending to a File That Doesn't Exist
>> will create the file if it doesn't exist. That's usually what you want, but be aware that it creates an empty file first.
Trailing Newline Issues
Some tools expect a newline at the end of a file. If you append without a newline, the last line of the old content and the first new line will be on the same line. Use echo (which adds a newline) or add \n explicitly.
Frequently Asked Questions
How do I append a line to a file without a newline?
Use echo -n "text" >> file or printf "text" >> file. This adds the text directly after the last character, so be careful about spacing.
Can I append to multiple files at once?
Yes, with tee -a file1 file2 or by using a loop. For example: for f in file1 file2; do echo "line" >> "$f"; done
What is the difference between > and >>?
> overwrites the file, truncating it to zero bytes first. >> appends, keeping existing content. This is fundamental to shell redirection.
How do I append a line to a file as root?
Use sudo sh -c 'echo "line" >> /path/to/file' or echo "line" | sudo tee -a /path/to/file. The second method is safer because it doesn't run a full shell as root.
Does appending work on binary files?
Yes, >> works on any file, but for binary data you should use dd or cat with care. The shell's redirection is byte-oriented, so it will append raw bytes.
Safety and Best Practices
- Always test with a dummy file first if you're unsure.
- Use absolute paths in scripts to avoid accidental writes to the wrong file.
- Make backups before appending to critical files, especially with
sed -i. - Avoid using
sudounless necessary; it can create files owned by root that you can't edit later. - For log files, consider using
loggeror systemd's journal instead of manual appends.
Appending lines is a basic but essential skill. With these methods, you can handle everything from quick notes to complex scripted log entries.