Jake Worth

Jake Worth

How to Append to a Write-Protected File

Published: October 19, 2022 2 min read

  • terminal

Suppose you want to append to a file that’s write-protected and can only be edited via sudo. Is that possible? It is!

A technique I discovered today is to use tee with sudo:

$ cat file.txt | sudo tee -a /etc/hosts

This lets me append the contents of file.txt to the write-protected /etc/hosts file by entering my password.


Get better at programming by learning with me. Subscribe to my newsletter for weekly ideas, creations, and curated resources from across the world of programming.