58

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?

6
  • 2
    If you replace the tee with cat >filename, do you get the results that you want?
    – sawdust
    Nov 11, 2012 at 23:12
  • 6
    cat does not write to files by itself. And the > operator doesn't work as the files are not writable by my current user. So no, that doesn't help a bit. And please stop to completely alter your comment's meaning every 2 seconds. Make a new one instead.
    – aef
    Nov 11, 2012 at 23:14
  • 7
    No, the > operator uses the current shell user to write to the file. This user hasn't got the neccessary permissions. cat is completely useless for that.
    – aef
    Nov 11, 2012 at 23:23
  • 3
    Also see stackoverflow.com/questions/82256/… Apr 26, 2013 at 8:10
  • 3
    Re: tee also sends its input to STDOUT again, I never ever actually used that part of tee for anything useful: It's very useful for debugging complex pipelines. Just insert | tee /dev/tty | at any point, to examinate the text going through that pipe. Example: echo foobar | sed s/oo/u/ | tee /dev/tty | sed s/r/z/ > /dev/null, which will output fubar ;) Jan 20, 2021 at 13:38

9 Answers 9

41

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

sudo command | sudo dd of=FILENAME
1
19

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.

2
  • 1
    dd prints junk to stderr. Feb 7, 2020 at 23:01
  • 1
    ^This: It's too bad tee doesn't have an option to suppress stdout.
    – ryenus
    Dec 7, 2022 at 14:47
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

2
  • 1
    Writing a script and deploying it to all the systems I work on is not exactly what I wanted, but until someone names an already existing tool, I think I'll go with your solution.
    – aef
    Nov 12, 2012 at 0:07
  • 2
    Indeed. Would like to see it myself. I wrote a program in C a long time back that did what you ask, but it is a long time gone and forgotten. There was close to no interest for it back when. But then again I did not actually advertise it much.
    – Luca Stein
    Nov 12, 2012 at 17:59
6

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.

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

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.

-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 ""
3
  • Can you elaborate further what this command is doing?
    – Vylix
    Jun 7, 2017 at 1:26
  • 2
    Same as tee >/dev/null, but instead piping to a grep that matches nothing. Aug 16, 2017 at 10:58
  • 1
    That sounds even more hacky than redirecting the output to /dev/null, and doesn't make the command any shorter. What would be the advantage of using this instead of the original method?
    – waldyrious
    May 30, 2018 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .