1

If I have a file

$ touch some_file.txt

And I want to move it to a directory which requires sudo permissions

$ cp some_file.txt /usr/local/bin

I get an error:

cp: cannot create regular file ‘/usr/local/bin/some_file.txt’: Permission denied

Then I have to prefix the command with sudo in order for it to work

$ sudo cp some_file.txt /usr/local/bin

How can I automatically get a sudo password prompt when I execute a command that needs permissions?

Something like:

$ cp some_file.txt /usr/local/bin
cp: cannot create regular file ‘/usr/local/bin/some_file.txt’: Permission denied
Please enter sudo password:
************ # Entering my password
# Command executed successfully
  • 2
    I've no idea (I imagine you'd have to recompile cp with modifications, but I am not very smart), but you can enter sudo !! to run the last command with sudo instead of re-typing the command or pressing up and editing – Zanna Jan 18 '17 at 11:37
7

While this might be possible, it is a really, really bad idea. This would encourage you to just blindly run things with sudo instead of investigating why you don't have permissions and what you're actually trying to do. In some cases, this could lead you to use sudo where not necessary and could, for example, cause you to change the ownership of config files etc.

Instead of just blindly asking for the sudo password, you can use bash's history shortcuts and run:

sudo !!

The !! is expanded to the previous command you ran. For example:

$ whoami
terdon
$ sudo !!
sudo whoami
root

At least, this way, you can see what's going on. It is only a tiny bit more complex than what you asked for and it is a tiny bit safer.

  • I've updated the question to include the full error message. I don't want to ignore the 'why'. I want to save the mental surplus of typing the command with sudo or typing sudo !!. I know it doesn't seems like a big deal but it's still annoying. – Eyal Levin Jan 18 '17 at 13:13
  • 2
    @EyalLevin I know. My point is that this is bad practice. If you implemented something like this, that would mean that any permission denied error would always, automatically just ask you for your sudo password. I get how this could be helpful sometimes, I'm trying to point out that it can also be very dangerous sometimes. There is often a very good reason why you don't have permission so simply repeating the command with sudo is not always the best approach. – terdon Jan 18 '17 at 13:39
  • So maybe some solution that could be restricted for some commands like cp. It rarely happens that I'm trying to copy something and because a permission issue I would change my mind. 99% of the times I would use sudo to cp the file. And in the 1% that I don't want, I don't think that this auto-prompt will cause me to accidentally copy it. – Eyal Levin Jan 18 '17 at 13:45

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.