6

I'm trying to create and fill a file in a directory that requires sudo permission. I've tried:

$ echo 'hello' > sudo tee /data/hello

However, I just get the output that /etc/file does not exist:

$ cat /data/hello
cat: /data/hello: No such file or directory
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
richard_d_sim
  • 793
  • 2
  • 10
  • 23
  • 3
    can't reproduce that – hek2mgl Mar 15 '17 at 13:22
  • 1
    What is the exact output? `tee` should create the destination file for you. – Attie Mar 15 '17 at 13:23
  • 1
    If you don't have `/etc` that would explain it, but this seems unreproducible on any reasonably regular system. – tripleee Mar 15 '17 at 13:26
  • 1
    Are you sure you **actually** tested with the **exact** command? If you ran `sudo tee /etc/some/file`, and `/etc/some` didn't exist, that would have the behavior described. `sudo tee /etc/file`, by contrast, is a completely legitimate way to escalate privileges used to write to `/etc/file`. I'd suggest reproducing the issue with `set -x` enabled, and incorporating that *exact*, unedited log into the question. – Charles Duffy Mar 15 '17 at 13:44
  • Ahh! Thank you for editing in your actual command; that makes this answerable. – Charles Duffy Mar 15 '17 at 13:49
  • I updated it. Thanks for the direct link, but I had to dig a bit as the redirected question wasn't specifically the same. – richard_d_sim Mar 15 '17 at 13:49
  • ...btw, try running `ls -l sudo` to demonstrate that your command given in the log created a `sudo` file when run; you might also run `cat sudo` to view its contents, which should be the string `hello tee /data/hello` (as these arguments were passed to `echo`, not run as a separate command). – Charles Duffy Mar 15 '17 at 13:52
  • I just tired that and it say no such file or directory – richard_d_sim Mar 15 '17 at 13:56
  • Has to be in the same directory (and Vagrant guest instance, if applicable), where you also ran `echo 'hello' > sudo tee /data/hello`. (It looks like that was `~` from the log, so `cat ~/sudo` if logged into that same account and instance). – Charles Duffy Mar 15 '17 at 13:57
  • ohh ok, that's pretty neat. – richard_d_sim Mar 15 '17 at 14:01
  • ...so, right now, you've got an internally contradictory question -- one part of it says command A failed, one part says you actually used command B. It should probably be edited to be consistent, or closed as being result of a typo. – Charles Duffy Mar 15 '17 at 14:06
  • Ah, Thanks for the heads up, I'll edit it as it is confusing. Thanks for being so helpful. stackoverflow is a bit rough to approach. – richard_d_sim Mar 15 '17 at 14:17
  • ...unfortunately, the current edit set goes as far as to invalidate existing answers -- that's rather explicitly not kosher here. Indeed, it doesn't leave any question at all, but appears to be only an answer itself. If there *is* no remaining question, the appropriate action is just to delete the question -- which will also have the side effect of refunding reputation lost to downvotes. – Charles Duffy Mar 15 '17 at 14:45
  • Rolled back edits that removed context needed to make answers understandable. **Answers or solutions do not belong in the question itself -- the question should be a question, the answers should be answers.** If you want to add your own answer, the way to do that is with the _Add an Answer_ button. – Charles Duffy Aug 10 '23 at 15:00

3 Answers3

12

The question has been edited to include a log of the problem being introduced, which includes the command:

# Taken from edited question
$ echo 'hello' > sudo tee /data/hello

In bash (which allows redirection operators at any point in a simple command), this is precisely equivalent to running:

# From question, with redirection moved to end to make actual behavior more readable
echo 'hello' tee /data/hello > sudo

That is, it's creating a file named sudo in your current directory, not using sudo to run tee (in the above, tee is just an argument to echo).

What you want, by contrast, is:

# CORRECT: Using a pipe, not a redirection
echo 'hello' | sudo tee /data/hello

with a pipe (|) rather than a >.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Assuming you have the required sudo privileges you could use an editor like vi or nano

sudo vi /etc/file

or

sudo nano /etc/file

If you don't have sudo for those programs you can su to root first and then try:

sudo su -

vi /etc/file

or

nano /etc/file
GreensterRox
  • 6,432
  • 2
  • 27
  • 30
0

If you want to do it all on one command line, you can make sure that the file exists by running touch(1) on it. E.g.

sudo touch /etc/file && echo "Some text" | sudo tee /etc/file

vielmetti
  • 1,864
  • 16
  • 23