The Computer Oracle

Alternative to the tee command without STDOUT

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping

--

Chapters
00:00 Alternative To The Tee Command Without Stdout
00:45 Accepted Answer Score 41
01:01 Answer 2 Score 19
01:39 Answer 3 Score 6
02:11 Answer 4 Score 6
02:44 Answer 5 Score 1
02:59 Thank you

--

Full question
https://superuser.com/questions/504343/a...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #commandline #posix #tee

#avk47



ACCEPTED ANSWER

Score 41


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

sudo command | sudo dd of=FILENAME



ANSWER 2

Score 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.




ANSWER 3

Score 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




ANSWER 4

Score 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.




ANSWER 5

Score 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'