-2

I ran the following command, hoping to be able to paste a file's contents into it.

sudo cat > /usr/bin/sasquatch

And keep getting the following error, even after changing the file permissions with sudo.

bash: /usr/bin/sasquatch: Permission denied

Why is this happening, and how can I fix it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
baranskistad
  • 2,176
  • 1
  • 21
  • 42
  • Sudo only grants you the access that the admin gave you. It's not generally unstoppable root magic unless it's your own personal machine. – Mad Physicist Dec 05 '16 at 02:43
  • 2
    Use `sudo sh -c 'cat > /usr/bin/sasquatch'` Remember, in your case, `sudo` only applies to `cat`, the redirection `>` is performed as the regular user. Using `-c` takes care of that. – David C. Rankin Dec 05 '16 at 02:45
  • Note that it is a bad idea to go tampering with files in `/usr/bin` — you should leave files in there alone (and those in `/bin` if it is different from `/usr/bin`). If you need to add files to a 'system' directory, it is normally better to use `/usr/local/bin`. – Jonathan Leffler Dec 05 '16 at 03:11

1 Answers1

2

> will make the shell open a file for writing. Your shell is running with your user permissions.

Using sudo will only make the command run elevated, and not the shell (which is opening the file).

One way is to start a new shell with sudo:

sudo bash -c 'cat > /usr/bin/sasquatch'

Since you are mentioning pasting, you can consider using xclip:

sudo bash -c 'xclip -sel clip > /usr/bin/sasquatch'
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123