Search code examples
bashfile-permissionssudocat

Can't cat to a file in /usr/bin


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?


Solution

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