144

What is the easiest way to append text to a file in Linux?

I had a look at this question, but the accepted answer uses an additional program (sed) I'm sure there should be an easier way with echo or similar.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Dchris
  • 2,867
  • 10
  • 42
  • 73

4 Answers4

229

How about:

echo "hello" >> <filename>

Using the >> operator will append data at the end of the file, while using the > will overwrite the contents of the file if already existing.

You could also use printf in the same way:

printf "hello" >> <filename>

Note that it can be dangerous to use the above. For instance if you already have a file and you need to append data to the end of the file and you forget to add the last > all data in the file will be destroyed. You can change this behavior by setting the noclobber variable in your .bashrc:

set -o noclobber

Now when you try to do echo "hello" > file.txt you will get a warning saying cannot overwrite existing file.

To force writing to the file you must now use the special syntax:

echo "hello" >| <filename>

You should also know that by default echo adds a trailing new-line character which can be suppressed by using the -n flag:

echo -n "hello" >> <filename>

References

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • 4
    In general, I like this more than the accepted answer that uses `cat`, but you'd better be careful with making sure to use `>>` instead of `>`! – sixty4bit Apr 20 '16 at 20:56
  • @sixty4bit - Totally agree, but I think you always must know what you're doing when working with a shell or else you might do something like `sudo rm -r /` =) – Cyclonecode Apr 20 '16 at 21:03
  • 1
    better `sudo rm -r /` than `sudo rm -rf /`! ^_^ Is there a flag that would ask for confirmation before overwriting the contents of an existing file with `>`? Aliasing that to always use such a flag seems like it would be a good idea. – sixty4bit Apr 21 '16 at 12:37
  • 2
    @sixty4bit - You can add `set -o noclobber` in your `.bashrc` then you cannot overwrite an existing file without using the special syntax `echo "hello" >| ` – Cyclonecode Apr 21 '16 at 15:23
  • can you give an example where the file is called, say "myFile.txt" or something? I'm not sure if it's or filename in your example – bharal Apr 03 '18 at 20:35
  • is just a placeholder for a filename so it should be echo ”hello” >> myfile.txt for example. – Cyclonecode Apr 03 '18 at 21:30
  • If I wanted to do this, but for a file that requires `sudo` permissions to write to, how would you write that command out? EDIT: Nevermind, found out how to do it using `tee` https://superuser.com/a/136653/59729 – NessDan Jul 06 '18 at 15:48
  • `sudo bash -c "echo hello >> "` =) – Cyclonecode Mar 21 '19 at 08:30
147
cat >> filename
This is text, perhaps pasted in from some other source.
Or else entered at the keyboard, doesn't matter. 
^D

Essentially, you can dump any text you want into the file. CTRL-D sends an end-of-file signal, which terminates input and returns you to the shell.

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • 2
    How can i do this when the file need root permission? – UselesssCat Jul 18 '17 at 13:52
  • 3
    @UselesssCat when something goes "permission denied", I re-check whatever command I'm running and then use `sudo !!` - which executes the last command with `sudo` prepended. I recently found out this also works with `nano` etc (by accidentally trying to `nano nano auth`...) – Sandra May 11 '18 at 23:23
  • if you need to use sudo or use this in a script refer to my [follow up to this accepted answer](https://stackoverflow.com/a/43423683/1048805) below. – user12345 May 25 '18 at 06:28
  • 5
    I didn't know cat could be used like this! Like writing a journal entry. Also didn't know about what ctrl-d does. Thank you! – houallet Aug 15 '18 at 01:10
  • 1
    @Sandra - Did not use `!!` before, awesome. So often I do stuff like `cat /var/log/apache/error.log` and get permission denied. This could also be done with an id from the history e.g `sudo !1419`, this will execute the command at line `1419` in the history as sudo. – Cyclonecode Aug 22 '19 at 08:51
29

Other possible way is:

echo "text" | tee -a filename >/dev/null

The -a will append at the end of the file.

If needing sudo, use:

echo "text" | sudo tee -a filename >/dev/null
Asclepius
  • 57,944
  • 17
  • 167
  • 143
user9869932
  • 6,571
  • 3
  • 55
  • 49
22

Follow up to accepted answer.

You need something other than CTRL-D to designate the end if using this in a script. Try this instead:

cat << EOF >> filename
This is text entered via the keyboard or via a script.
EOF

This will append text to the stated file (not including "EOF").

It utilizes a here document (or heredoc).

However if you need sudo to append to the stated file, you will run into trouble utilizing a heredoc due to I/O redirection if you're typing directly on the command line.

This variation will work when you are typing directly on the command line:

sudo sh -c 'cat << EOF >> filename
This is text entered via the keyboard.
EOF'

Or you can use tee instead to avoid the command line sudo issue seen when using the heredoc with cat:

tee -a filename << EOF
This is text entered via the keyboard or via a script.
EOF
user12345
  • 2,876
  • 2
  • 24
  • 25