Tibor's Musings

Tee

Have you ever tried to use echo with sudo to write some text to files owned by another user? Did not work? Tee, a handy tool that reads from standard input and writes to standard output and files, is here help.

Imagine the following directory permission situation:

$ ls -ld /var/www
drwxr-xr-x 3 www-data www-data 4096 Dec 15 14:09 /var/www

This does not work:

$ echo Hello > /var/www/index.html
bash: /var/www/index.html: Permission denied

This does not either:

$ sudo -u www-data echo Hello > /var/www/index.html
bash: /var/www/index.html: Permission denied

The solution is to use tee:

$ echo Hello | sudo -u www-data tee -a /var/www/index.html
Hello

Another, possibly more common example, involving super user:

$ cat /sys/module/video/parameters/brightness_switch_enabled
Y
$ echo N | sudo tee /sys/module/video/parameters/brightness_switch_enabled
N
$ cat /sys/module/video/parameters/brightness_switch_enabled
N

unix