Jake Worth

TIL: Append to a Write-Protected File

Published: October 19, 2022 2 min read

  • til

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 Jake Worth's Newsletter for bi-weekly ideas, creations, and curated resources from across the world of programming. Join me today!


Blog of Jake Worth, software engineer in Maine.

© 2022 Jake Worth.