62

I'm using | sudo tee FILENAME to be able to write or append to a file for which superuser permissions are required quite often.

Although I understand why it is helpful in some situation, that tee also sends its input to STDOUT again, I never ever actually used that part of tee for anything useful. In most situations, this feature only causes my screen to be filled with unwanted jitter, if I don't go the extra step and manually silence it with tee 1> /dev/null.

My question: Is there is a command arround, which does exactly the same thing as tee, but does by default not output anything to STDOUT?

Andrea
  • 1,536
aef
  • 1,502
  • 4
  • 18
  • 20

9 Answers9

44

Another option that avoids piping the stuff back and then to /dev/zero is

sudo command | sudo dd of=FILENAME
21

The dd solution still prints junk to stderr:

$ ls | sudo dd of=FILENAME
0+1 records in
0+1 records out
459 bytes (459 B) copied, 8.2492e-05 s, 5.6 MB/s

That can be avoided using the status option:

command | sudo dd status=none of=FILENAME

Another interesting possibility (for Linux anyway):

command | sudo cp /dev/stdin FILENAME

To copy TTY input into a file, I often do this:

sudo cp /dev/tty FILENAME

It's too bad tee doesn't have an option to suppress stdout.

Curt
  • 396
7

I would add a sponge as an alternative.

On Ubuntu or other Debian based distributions, you can install it with sudo apt install -y moreutils

Please note that there are some differences compared to tee about which you can read more here and here

Unlike a shell redirect, sponge soaks up all its input before writing the output file. This allows constructing pipelines that read from and write to the same file.

This difference is actually an advantage for my typical use cases.

Vladan
  • 187
6

You could use a script. I.e. put something like this in i.e. $HOME/bin/stee, 0tee or similar:

#!/bin/bash

argv=
while [[ "$1" =~ ^- ]]; do
    argv+=" $1"
    shift
done

sudo tee $argv "$1" > /dev/null

#!/bin/bash

sudo tee "$@" > /dev/null

Make it executeable:

$ chmod 755 stee

Now do i.e.:

$ ls -la | stee -a /root/foo

Luca Stein
  • 91
  • 1
  • 6
1

You can wrap your whole command into sudo, so the shell itself, as well as redirects, are performed as root:

sudo sh -c 'do_something > FILENAME'
0

attempt to create a file against root owned directory

$ ls -la /opt/ | head -2
total 0
drwxr-xr-x   3 root  wheel   96 Jun 13  2019 .
$ touch /opt/bufu
touch: /opt/bufu: Permission denied

create file using pesky redirection, log event, and write file content to stdout, in somewhat parallel, certainly concurrent.

$ cat /tmp/test.sh
#!/bin/sh

file=/opt/bufu
message="an embarrassing failure"
rm -rf $file

{ echo $file \
    | tee /dev/fd/2 \
    | xargs sudo sh -c 'echo $1 >$2' _ "$message"
} 2>&1 \
    |
{ tee /dev/fd/3 \
    | xargs logger -s "Create File"

} 3>&1 \
    |
{ xargs cat \
    | xargs -I% echo file content is "%"
}
$ /tmp/test.sh
rm: /opt/bufu: Permission denied
Feb  7 17:03:16  bufu[4039] <Notice>: Create File /opt/bufu
file content is an embarrassing failure

dude, it's not tee's fault that its being forced into an uncomfortable position - use xargs sh, or write your own one-liner

cat <<eof >./aef
echo "$1" >$2
eof
..
sudo aef "is a cool guy" /opt/fubar
0

I have written a tee equivalent in Go that I added the no stdout flag to. It may be of interest (though this is an old question). It was written for fun so I make no claims as to its suitability for a production purpose.

https://github.com/imarsman/gotee

-1

I know is a little too late, but what I do in such cases (when a 'flat' stdout but also a temporary file is needed) is:

tee whatever | grep -v ""
music2myear
  • 49,799
-1

There is not directly a program to do that (this is pretty much the only time that it would be useful), but you could easily write your own. If you do not want to program, you could also write a simple shell script that does the same thing: cat > $1. This is different from putting it inline (as sawdust suggested) because the sudo will apply to the entire script, including the redirection.