Append to a File in Linux Using cat – with Examples

Updated

append to file with cat

To append one file to another, redirect cat with >>. To append typed or scripted text, give cat no filename at all and feed it a heredoc:

cat extra.txt >> target.txt        # append a file

cat >> target.txt <<'EOF'          # append literal text
listen 8080;
root $HOME/site;
EOF

Those quotes around EOF are the single most consequential character in this article, and the reason most heredoc bugs happen. Everything below is executed output, not description.

<<EOF expands variables. <<'EOF' does not.

Both blocks below append the same two lines to a copy of the same starting file. Only the quoting differs:

cat >> a.conf <<EOF
log_format main '$remote_addr "$request"';
root $HOME/site;
EOF

cat >> b.conf <<'EOF'
log_format main '$remote_addr "$request"';
root $HOME/site;
EOF

The results are not the same file:

===== unquoted  <<EOF
# base config
log_format main ' ""';
root /Users/[user]/site;
===== quoted    <<'EOF'
# base config
log_format main '$remote_addr "$request"';
root $HOME/site;

(Only the account name is edited above: the unquoted heredoc really did expand $HOME to a real path, which is the whole point of the comparison. Everything else is unaltered.)

In the unquoted version the shell expanded $remote_addr and $request — variables that do not exist in the shell, because they belong to nginx — to empty strings. You now have log_format main ' ""'; in a config file. Nothing errored. Nothing warned. The file is 60 bytes where it should be 74.

The rule: quote the delimiter unless you specifically want the shell to substitute values. When you do want substitution, use the unquoted form and escape the dollars you want kept literal (\$remote_addr). Anything with $, backticks, or $(...) that is meant for another program — nginx, systemd, Make, awk, crontab, another shell script — wants <<'EOF'.

<<-EOF strips leading tabs so you can indent the heredoc inside an if block. It does not strip spaces, which is why it appears not to work in an editor set to soft tabs:

x
tab-indented line
    space-indented line

Appending interactively, and the one-line footgun

cat >> notes.txt with no input file reads your terminal until you press Ctrl-D on an empty line. That is genuinely handy for a quick note. What is not handy:

$ cat notes.txt > notes.txt
$ echo $?
0
$ wc -c notes.txt
       0 notes.txt

Exit status 0, file destroyed. The shell opens and truncates the target before cat starts reading it. And the append form does not save you — it never terminates, because cat keeps reading the bytes it just wrote. Run under a file-size limit so it dies instead of filling the disk:

$ bash -c 'ulimit -f 40; cat u.txt >> u.txt'
bash: line 1: 83445 Filesize limit exceeded: 25cat u.txt >> u.txt
$ wc -c u.txt
   40960 u.txt          # it started at 8 bytes

If source and destination are the same file, use a temporary file.

Why sudo echo x >> /etc/file fails and | sudo tee -a works

Because the shell performs the redirection, not the command. sudo elevates echo; it never sees the >>, which your unprivileged shell has already carried out.

You can watch this happen without any root involvement. Here is a directory the current user cannot write to:

$ bash -c 'echo hello >> locked/out.txt'
bash: line 1: locked/out.txt: Permission denied
$ echo $?
1

Now run the same redirection against a command that does not exist:

$ bash -c 'definitely_not_a_command hello >> locked/out.txt'
bash: line 1: locked/out.txt: Permission denied

Same error. There was no command to run. The shell opened the file first and gave up before it ever looked for the program — which is precisely why putting sudo in front of the program changes nothing.

Pipe into tee instead, and the process holding the file descriptor is tee itself. Look at who reports the failure:

$ echo hello | tee -a locked/out.txt
tee: locked/out.txt: Permission denied
hello

The prefix changed from bash: to tee:. That prefix is the whole explanation: tee is the process that calls open(), so sudo tee calls it as root and succeeds.

echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
cat extraconfig.txt        | sudo tee -a /etc/myapp.conf

Add > /dev/null if you do not want the text echoed back — tee writes to stdout by design, which is what makes it work in a pipeline. tee -a also takes several destinations at once:

$ echo "shared line" | tee -a m1.txt m2.txt > /dev/null
$ cat m1.txt m2.txt
shared line
shared line

The sudo tee lines above are the one thing on this page that was not executed: this machine has no passwordless sudo, so writing to /etc was not attempted. The permission mechanism they rely on is what the locked/ output demonstrates directly. The heredoc, truncation and permission-denied results above are re-checked by append-to-file-with-cat-command.sh, which works in a scratch directory it deletes on exit and reports the sudo tee step as skipped rather than faking it.

Appending strings in bash scripts

For the difference between > and >>, why printf beats echo in scripts, and a measured demonstration that >> is safe under concurrent writers, see appending strings to a file in bash.

Tested on macOS 26.6 (arm64) with GNU bash 5.3.15 and BSD tee. This is POSIX shell behaviour and is identical on Linux; the only environment-specific text is the Filesize limit exceeded wording, which varies between shells.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.