13

I have two cases where I want to create a file with its contents supplied on standard input:

  1. I need to use sudo to have the privileges to create a file.
  2. An application always invokes an (interactive) editor specified by $EDITOR to process this input further, but I want to pipe data from a program instead.

In the former case, I could use echo test | sudo bash -c 'cat > test.txt', but in this as well as in the latter case I usually use tee and direct tee's standard output to /dev/null:

  1. echo test | sudo tee test.txt > /dev/null
  2. echo test | EDITOR=tee application > /dev/null

However, this does not only throw away tee's standard output, but also (in the latter case) application's. It would be preferable to be able to specifically suppress tee's standard output, so that I could instantly notice if something else wrote to standard output.

Is there a utility on standard Linux distributions that will write standard input to the file specified as a command line argument and not output anything?

2
  • This question sounds like an "XY Problem". What are you really trying to do?
    – Spiff
    Commented Aug 11, 2015 at 4:22
  • 1
    No, this is typical when using sudo to write a file : you want the write handled by sudo, hence the shell redirection is not usable here. Legit question, and legit answer, as sed is definitely a standard utility. Commented May 20, 2020 at 11:30

1 Answer 1

7

There's the w command of sed:

sed -n 'w /tmp/out'

But perhaps you should just write your own script mycat:

#!/bin/bash
cat >"${1?}"

Create a directory ~/bin and add it to the front of your PATH so your command is found.

You may say, "but I use hundreds of machines, they won't have this script on them". In that case my suggestion is that you create a script that simply sets up your preferred environment on any machine you want. I have a script that will, ideally when I have root access, create a new user id, add a sudoers entry for it, copy some .bashrc and bin/* files, and so on. Then I can login under that id and "feel at home". Also, my command history is then not polluted by other admins doing root logins, and I am not doing all my commands as root.

I often have to work on brand new OS installations on hardware under test, and this sort of automatic setup is a real blessing.

2
  • 2
    Unfortunately this does not address my question. I'm looking for a standard utility that takes one argument and writes stdin to it. I know how to write wrapper scripts, deploy software, etc.; avoiding that effort is the point of this question. Commented Dec 2, 2016 at 1:06
  • 2
    I though sed -n 'w /tmp/out' was pretty cool.
    – chnrxn
    Commented Dec 26, 2018 at 11:48

You must log in to answer this question.

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