35

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?

  • If you replace the tee with cat >filename, do you get the results that you want? – sawdust Nov 11 '12 at 23:12
  • 4
    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 '12 at 23:14
  • If you replace the | sudo tee FILENAME with | sudo cat >filename, do you get the results that you want? – sawdust Nov 11 '12 at 23:20
  • 4
    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 '12 at 23:23
  • 2
    Also see stackoverflow.com/questions/82256/… – Joachim Breitner Apr 26 '13 at 8:10
27

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

sudo command | sudo dd of=FILENAME
  • 1
    Exactly what I was looking for. Great thanks. – aef Sep 7 '13 at 14:36
  • 4
    superuser.com/a/850294/68284 describes the additional oflag=append conv=notrunc options to use dd for appending. – aef Jan 22 '16 at 9:32
6

The dd solution still prints junk to stdout:

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

4

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

  • 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 '12 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 '12 at 17:59
2

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

-2

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 ""
  • Can you elaborate further what this command is doing? – Vylix Jun 7 '17 at 1:26
  • Same as tee >/dev/null, but instead piping to a grep that matches nothing. – 00prometheus Aug 16 '17 at 10:58
  • 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 '18 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.