Possible solutions could be:

  1. Starting interactive session.

    sudo -s <<< "cd dir ; command"


    sudo /bin/bash -c "cd dir ; command"

    But I don't have /bin/bash /bin/sh /bin/su or other sudoer permissions

  2. Changing directory before sudo is done.

    cd dir ; sudo command

    But I don't have permission to enter the dir.

  3. I need a general case pwd set (like Popen cwd), so below is not answer I can use:

    sudo command /path/to/file

What I'm trying to write is python Popen wrapper with sudo=True/False option, and currently I'm trying to somehow get cwd parameter to work.

  • so run sudo cd && sudo command. – Tim Nov 27 '14 at 12:28
  • sudo: cd: command not found. sudo -i cd /root doesn't work as well – Andrew Onischuk Nov 27 '14 at 12:29
  • 1
    What are you trying to achieve in the first place? Can't you run command on something in the directory? E.g. if command were grep regex file, just run sudo grep regex dir/file. Please provide more information in your question on the bigger picture here. And please edit your question for that rather than posting comments. :-) – gertvdijk Nov 27 '14 at 12:31
  • Whenever i need to run a command in a root directory, I use command /path/to/dir – Tim Nov 27 '14 at 12:35
  • Ah, your edit you mention you're doing some coding in Python. Please share a minimal working example so we can help you better. :-) – gertvdijk Nov 27 '14 at 12:47

I'm not sure what the bigger picture is, but your approach should be to run the command with on a file in the directory. E.g. if you want to run grep regex file where file is in /root, then you should approach it like this:

$ sudo grep regex /root/file

And not:

$ sudo 'cd /root; grep regex file'
sudo: cd /root; grep regex file: command not found

Why this output? Well, it's shell syntax and sudo isn't running the command in another interactive shell.

Another approach would be to alter the environment variable PWD, like this:

$ sudo -i PWD=/root grep regex file
| improve this answer | |
  • The first approach I cannot use to set pwd in general case. (Added the bigger picture to the question) The second doesnt work for me since, it is not configured as you said (in general case for the user of our software) – Andrew Onischuk Nov 27 '14 at 12:43
  • @AndrewOnischuk yes, second approach doesn't work, you're right. removed it. – gertvdijk Nov 27 '14 at 12:45
  • 1
    That actually works with -i specified sudo -i PWD=/root ls, thanks. Exactly what I was looking for – Andrew Onischuk Nov 27 '14 at 13:07
  • A practical use case is when you need to run "make install" in a directory you don't own, e.g., in the home directory of an unprivileged user. (The alternative in that case would be to use sudo make -C ~unprivileged/project install.) – sffc Feb 22 '16 at 14:34

For me a combination of sudo and screen worked out:

sudo -iu vagrant screen -mS npm_install bash -c 'cd /vagrant && npm install'

This command first switches to the vagrant user. Then as vagrant changes the directory to /vagrant and executes npm install.

| improve this answer | |

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.