Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why typing sudo cd whatever won't change the directory?

share|improve this question
2  
see askubuntu.com/questions/291666/… – John Sep 3 '13 at 13:08
up vote 40 down vote accepted

cd is a shell builtin. sudo only works with executables. You could do sudo sh -c 'cd dirname' but as soon as the shell exits, you're returned to the directory you started from. If you say what it is you're trying to accomplish then I can help you find a way to do that.

share|improve this answer
1  
But then sudo pwd wouldn't work either? (I always figured that sudo cd does work, but you're just not seeing the result after sudo returns. But that was just a wild guess. Maybe neither cd nor pwd are actually built-in in Bash on a Mac. Running which cd does indeed give me results. Running sudo cd / does not give me an error, but indeed does not result in a changed working directory.) – Arjan Feb 3 '11 at 9:00
2  
@Arjan: pwd is also an external executable so it will work. Note that on some systems, there is a cd executable, but it's mostly useless. Try using type -a cd it's much more informative than which, by the way. – Dennis Williamson Feb 3 '11 at 9:06
1  
Nice! type -a cd shows both cd is a shell builtin and cd is /usr/bin/cd on my Mac. And likewise for pwd and echo. And both sudo pwd and sudo echo "Hello world" do give me a result. However, type -a return only yields return is a shell builtin, and sudo return 3 shows me sudo: return: command not found. So, I guess the question is: does the OP get an error message, or does the OP not see the cd work without any error? (Or: what OS is the OP using.) – Arjan Feb 3 '11 at 9:12
    
sudo sh -c 'cd dirname' doesn't do anything for me. – Peter Niederwieser Aug 9 '11 at 14:43
    
@Peter: See the part of my answer which begins "but..." – Dennis Williamson Aug 11 '11 at 22:08

Instead try using sudo -s to start a root shell and then simply cd into the directory.
When you're done as root, press CtrlD or type exit.


As Arjan hints at in his comment below, it is important to note that as root, one can easily do damage to essential system components. Use with care!

share|improve this answer
4  
But: be careful, once one is root... – Arjan Feb 3 '11 at 9:14

You can simply su to become root and then cd all you want... I know an answer has already been accepted, but if one is not on the sudoers list then this is the only option.

share|improve this answer
    
On the other hand, if there is no root password, or you don't know it, sudo is the only option. – Liam Oct 20 '15 at 18:37

I stumbled upon this question when I faced the same issue. I noticed later that the permissions of these folders were set a bit weird. Can't remember what it was exactly, but something like

drwx--x---

while root was both owner and group owner.

So if you want to keep using sudo and not just act as root as oKtosiTe is suggesting, I recommend running

ls -l

To check permissions (might get Permission Denied. Prefix with sudo in that case). And then

sudo chmod 755 [dir]

Or something and you should be able to enter the folder again with sudo.

share|improve this answer
4  
This doesn't answer the question of why "sudo cd" doesn't change the current working directory as visible to the user. Also, no, no, no, no! Never, ever blindly set permissions recursively, unless you know exactly what you are doing; it's almost always a bad idea. – Michael Kjörling Mar 17 at 10:44
    
Yes it does. It could be permissions. I've edited out the recursiveness flag. In my case I knew what I was doing, but it might not be applicable for this case. Actually my answer is an excellent alternative to just opening a root shell as root. So you might not find my answer the way to go but I prompt you to remove (assumably) your downvote. – Ogier Schelvis Mar 17 at 11:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.