Why typing sudo cd whatever won't change the directory?
-
7see http://askubuntu.com/questions/291666/why-doesnt-sudo-cd-var-named-work – John Sep 03 '13 at 13:08
6 Answers
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!
- 9,588
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.
- 108,309
-
3But then
sudo pwdwouldn't work either? (I always figured thatsudo cddoes work, but you're just not seeing the result aftersudoreturns. But that was just a wild guess. Maybe neithercdnorpwdare actually built-in in Bash on a Mac. Runningwhich cddoes indeed give me results. Runningsudo cd /does not give me an error, but indeed does not result in a changed working directory.) – Arjan Feb 03 '11 at 09:00 -
2@Arjan:
pwdis also an external executable so it will work. Note that on some systems, there is acdexecutable, but it's mostly useless. Try usingtype -a cdit's much more informative thanwhich, by the way. – Dennis Williamson Feb 03 '11 at 09:06 -
2Nice!
type -a cdshows bothcd is a shell builtinandcd is /usr/bin/cdon my Mac. And likewise forpwdandecho. And bothsudo pwdandsudo echo "Hello world"do give me a result. However,type -a returnonly yieldsreturn is a shell builtin, andsudo return 3shows mesudo: return: command not found. So, I guess the question is: does the OP get an error message, or does the OP not see thecdwork without any error? (Or: what OS is the OP using.) – Arjan Feb 03 '11 at 09:12 -
4
-
-
@Dennis: I misunderstood that part. The answer would be easier to understand if
cd dirnamewas followed up with something (e.g.; ...). – Peter Niederwieser Aug 12 '11 at 00:50 -
@Arjan That there is an external command
cdin/usr/bin/cdmakes no sense, except the externalcddoes something different than the shellscd. But then, you would have twocdwith different command line syntax. Very odd. The commandcdchanges the current directory of the current process. An external command creates a new process, by definition. So it can only change its own, and terminate with no effect to the shell that started it. – Volker Siegel Oct 21 '19 at 10:25 -
1@Arjan I found the explanation: It is something like a deep compatibility hack. It does not do its function, but it has come of the side effects that the real
cdhas. And it can do nothing instead of failing when the current shell does not have acdcommand. Any shell that may ever be used by humans hascd, including/bin/sh. But a program that is, seen from the operating system kernel, a shell can be much simpler than that, even only a couple of lines of code. See What is the point of thecdexternal command? – Volker Siegel Oct 21 '19 at 10:35
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.
- 2,076
-
4On 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
-
1
I had the same issue when I was attempting to navigate to the root directory in Kali Linux and:
sudo su
allowed me to execute:
cd /
- 3,176
- 131
There are two ways that it "won't work", depending on your OS:
If your OS follows POSIX, then running
sudo cdwill cause the external command "cd" (usually located at/usr/bin/cd) to execute in a forked process as the root user. That process changes directories successfully. Once that process is done, you will be returned to your regular shell, which is still in the directory where it started.So
sudo cdruns without error, but does not change the current working directory of your current shell.(reference: this answer)
Otherwise, running
sudo cdwill cause the following to happen. Your computer will look through your PATH, trying to find an executable named "cd". It will not find one. (only the shell built-in command "cd" exists, and that is not an executable file). Hence, you get an error. (On Ubuntu, I get the error messagesudo: cd: command not found.)So
sudo cdruns with error.
- 260
you can typically switch to the root user bu typing "sudo sh" and cd into wherever
- 1,416