add to the end of a file

Add lines to a file without replacing it, also to a file only root can write.

  • files
  • shell

Steps

  1. echo 'new line' >> file.txt

    add a line at the end

  2. command >> log.txt 2>&1

    add a command's output and errors

  3. command | tee -a log.txt

    see it and add it to the file

  4. echo 'new line' | sudo tee -a /etc/some.conf

    add to a file only root can write

Notes

  • One > replaces the file; two, >>, add to it.
  • sudo echo x >> /etc/file fails: the redirection is done by your shell, not by sudo.

Commands used

  • tee
  • echo

Each has a page in lore: lore tee opens it.